I am trying to use $q and $http to get data from server and wait until data is retrieved. After data is retrieved i would want to manipulate it.
This is my service with method which fetches data from server:
application
.factory('service', function ($http,$q) {
return {
fetch: function(url) {
var deferred = $q.defer();
$http.get(url).success(function(data){
deferred.resolve(data);
}).error(function(){
deferred.reject("An error occured while fetching items");
});
return deferred.promise;
}
};
});
Controller that consumes data looks like this:
service.fetch(url).then(function(data){
$scope.data = data;
},
function(errorMessage){
$scope.error=errorMessage;
});
$scope.data = ...manipulate data
Problem is that angular manipulates data before it is fetched. Any suggestions how to solve this would be much appreciated.