I try to request some data in a service. I wanna make sure sure that the data is saved in a variable of the service, since due to state changes, my controller will reload all the time and I need the data only ONCE when the site loads.
app.service('resultDeals',['$translate','$cookies','$http',
function($translate,$cookies,$http) {
var currentOrigin = {};
var originsUser={};
return {
loadOrigins:function() {
$http.get('app/deals/deal.json').success(function(response){
console.log(response);
originsUser = response.data;
return originsUser
}).error(function(err){
console.log(err);
});
},
userOrigin:originsUser
};
}]);
and my controller looks as follows
console.log(resultDeals.loadOrigins());
$scope.updateOrigins=resultDeals.loadOrigins();
The problem is that resultDeals.loadOrigins() is always undefined.
I want to make sure to store the data obtained from loadOrigins() in a service variable and only access it from the controller when needed so that when the controller reloads the data does not have to be obtained anymore.
How can I do this?