I there,
I'm building an Angular.js app with the help of Restangular and angular-local-storage. I need to retrieve some data from a RESTFull service server and assign it to a $scope variable.
I would like to know how could I wait for all that data to load before loading it to my view (html).
Here's what I've done so far:
app.controller('InventoryController', function($scope, inventoryService) {
$scope.productList = inventoryService.getProduces();
console.log($scope.productList); // At that point, $scope.productList is null
});
app.service('inventoryService', function(entityService, browserStorageService) {
entityService.allUrl('product', entityService.getBaseUrl).getList().then(function(data){
console.log(data); // The data variable is not null here.
browserStorageService.set('producList', data);
});
this.getProduces = function() {
return browserStorageService.get('producList');
};
});
app.service('browserStorageService', function(localStorageService) {
localStorageService.clearAll();
return localStorageService;
});
app.service('entityService', function(Restangular) {
Restangular.setBaseUrl('http://localhost:8000/rest/');
return Restangular;
});
I'm not at all comfortable with the asynchronous nature of JavaScript, I'm sure it's pretty simple, but I can't get my head around what I can do to correct the situation.
The data is not loader into the page at the fist call made to the controller, but when I call it again without reloading the app, the data is there.
Thanks for your help!