Hello there! I am looking to retrieve data from a factory before any of my controllers are loaded. After some research, I discovered that it can be achieved using the resolve function in AngularJS:
angular.module('agent', ['ngRoute','ui.bootstrap','general_module', 'order_module','customer_module']).config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/customer', {
templateUrl: 'customer_module/partials/customer.php',
resolve:{
'MyServiceData':function(loginvalidation){
// MyServiceData will also be injectable in your controller, if you don't want this you could create a new promise with the $q service
var a=loginvalidation.check_usertype();
}}
}
);
$routeProvider.otherwise({redirectTo: '/customer'});
}]);
However, the issue is that I would need to repeat this for every single route. Is there a more efficient way in AngularJS to write the code once and load the service before any controller is initialized (without duplicating the code)?
Thank you!