javascript - AngularJS: Set private service variable in callback -


i'm trying this:

app.service('productsservice', ['$http', productsservice]);  function productsservice($http){      return {         getproducts: getproducts     }      var _products = [];      function getproducts(){          $http.get('http://localhost:4000')             .then(function(data){                 _products = data;             });         } } 

but @ then callback _products undefined variable.

what correct way set _products value callback?

you need set variable before return statement.

app.service('productsservice', ['$http', productsservice]);  function productsservice($http){     var _products = [];      return {         getproducts: getproducts     }      //var _products = []; never run      function getproducts(){          $http.get('http://localhost:4000')             .then(function(data){                 _products = data;             });         } } 

Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -