I need to call http service once before all templates are loaded ( using ui-routing), and returned data available in all controllers, is that possible?
$http.get('localhost;8080/some.json').then(function(d) {
return d;
};
If you want to run it once and make it available to all controllers, then create a service, say, CoreData, and call it from app.run() as follows
app.factory('CoreData',function($http) {
var data, defer;
return {
load: function() {
defer = $http.get('localhost;8080/some.json');
},
get: function() {
return defer;
}
};
})
.controller('CtrlA',function(CoreData) {
$scope.foo = CoreData.get();
})
.controller('CtrlB',function(CoreData) {
$scope.foo = CoreData.get();
})
.run(function(CoreData) {
CoreData.load();
});
If you are concerned about timing, you can do the above .get() as a promise with a single method on CoreData.
To handle the loading, we just pass the $http promise back, which is automatically populated by angular when resolved.