I have a service "OneTimeService" that has a "init" method.
// Within OneTimeService code
var this = self;
this.init = function() {
return $http..... function(data) {
self.data = data
}
}
Inside of each of my controllers that are associated with my routes, I have a : // Within some controller code OneTimeService.init().then(data) { $scope.somevariable = data.someattribute; // do other stuff }
The problem I have is, I have 10 different "routes". Each of them has the:
// Within every controller (assuming each route I have uses a different controller) code but injects the OneTimeService.
OneTimeService.init().then(data) {
$scope.somevariable = data.someattribute;
// do other stuff
}
Everytime I call "init()", it performs an $http request, when in actuality, all I want is to be able to call it ONE TIME EVER in my application $http request, then use the cached variable "self.data" from the service. The reason why I like the .then is it guarantees that I have "self.data" set within the OneTimeService prior to doing anything else. Are there alternatives?
What is the best way to do this?