In my angular application, I want to load some user specific configuration data from server, if user authorized. This data will use all over the application. I have implemented cookie base token authentication. How can I delay application until this data loading complete?
Asked
Active
Viewed 41 times
1 Answers
1
You can use resolve
in route. Example from ng-doc:
app.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/Book/:bookId', {
templateUrl: 'book.html',
controller: 'BookController',
resolve: {
// I will cause a 1 second delay
delay: function($q, $timeout) {
var delay = $q.defer();
$timeout(delay.resolve, 1000);
return delay.promise;
}
}
});
Instead of timeout promise you can return any promise what you wish.
In this case your controller will be started after route.resolve
promise resolving.
Also look at this answer. It looks like very relevant for your case.
-
If we use this approach, then we need to do something defined for all the routes right? Is there a way to do it in one place without rewriting it again and again? – Nuwan.Niroshana Feb 28 '17 at 03:51
-
Yes, you need set it in each route, because a set of resolving promises can be different in each route. But you can create a function or a service which creates a route with your params like url, template controller and predefined properties like authorization resolving if you need it everywhere – Antonio Feb 28 '17 at 05:37