0

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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.