Search code examples
javascriptangularjsangular-ui-routerangular-promiseresolve

Angularjs ui-router resolve in route


Could you check my code:

    .state('addNewGroup', {
    url: '/group/new',
    resolve:{
        "checkPermission": function ($state, checkPermission) {
            checkPermission.checkPermission()
                .then(function (result) {
                    if (result) {
                        alert('Hello');
                    } else {
                        alert('access denied');
                    }
                })
        },
        "otherStuff": function () {
            console.log("Should execute only if result in 'checkPremission' is true");
        }

    },
});

PROBLEM

I two promises in the routing configuration. Is it possible to resolve only specific promise? Let's suppose I have 5 promises to resolve and I want to resolve them all only if "checkPremission" is true. In other cases I don't want to resolve it all. Is it possible or I should use different logic.


Solution

  • You can make the next promise wait for the previous one by injecting it as a dependency:

    resolve: {
      firstResolve: function() {
        return 'A result';
      },
      nextResolve: function(firstResolve) {
        // firstResolve has the value 'A result' here
      }
    }
    

    Technically, the second resolve will execute, but you can base your logic on the result of the first one.