Search code examples
angularjsangular-routingresolveroute-provider

Angular Routing - Wait for previous resolve


I have an angular route as below:

    $routeProvider.when('/admin/:one/:two/:three?', {
        templateUrl: '/app/partials/admin.html',
        controller: 'AdminCtrl',
        resolve: {
            login: function(LoginService) {
                return LoginService.login();
            },
            admin: function($route, LoginService) {
                return LoginService.admin($route.current.params.one, $route.current.params.two, $route.current.params.three);
            }
        }
    });

The issue is that the second resolve, admin, depends on the first resolve completing (LoginService.admin() calls ensureAuthenticated, which requires LoginService.login() to complete else it just fails).

Is there a way to tell the admin resolve to wait for the login resolve?

Thanks for any help/advice you can provide.


Solution

  • If you inject the first resolve result (login in your case) to the the 2nd resolve, then that resolve will be delayed until that value is loaded. If it's not loaded or there is an error, it will not trigger:

    $stateProvider.state('/admin/:one/:two/:three?', {
            templateUrl: '/app/partials/admin.html',
            controller: 'AdminCtrl',
            resolve: {
                login: function(LoginService) {
                    return LoginService.login();
                },
                admin: function($route, LoginService, login) { //here we injected login
                    return LoginService.admin($route.current.params.one, $route.current.params.two, $route.current.params.three);
                }
            }
        });
    

    EDIT: Changed the answer, at the moment I only know this to work with $stateProvider.