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
Post a Comment