When it comes to the issue of Passing data between controllers in Angular JS?, I encountered a problem where my ProductService was returning NULL because the callback function had not completed.
The p.getProducts() function is being evaluated, but since the callback function responsible for fetching data from the RestFUL service hasn't finished, the function always returns null.
app.service('productService', function() {
p = this
p.productList = [];
var addProduct = function(newObj) {
productList.push(newObj);
}
p.getProducts = function(){
return $http(RestFUL Service,function(data){p.productList.push(data)});
}
return {
addProduct: addProduct,
getProducts: return p.getProducts();
};
});
How can this issue be resolved?