Skip to content Skip to sidebar Skip to footer

Angular Route Resolve Calling A Service

I'm struggling to get Angular route resolve working. I find the documentation less than useless for more complex parts of the javascript framework like this. I have the following s

Solution 1:

The resolve block when configuring routes is designed to make navigation conditional based upon the resolution or rejection of a promise.

You are attempting to handle this by resolving with a resolution value of true or false

Please try the following:

resolve: {
  auth: ["AuthService", function(AuthService) {
    returnAuthService.test().then(function(auth){
      if (!auth){
        throw'not authorized';
      }
    });
  }]
}

This will cause the promise to be rejected and therefore not allow the routing to continue/complete.

Also of note is that the value coming out of the promise resolution will be injected into the handling controller

Solution 2:

This solution works for me, I also tried with .then function but it's incorrect because resolve performs it.

resolve: {
auth:
    ["AuthService", "AnotherService", "$rootScope", function(AuthService, AnotherService, $rootScope) {
        if ($rootScope.yourCondition){
            return AuthService.getFunction();
        }
        else{
            return AnotherService.getAnotherFunction();
        }
    }]
},
views: {
'partialView@': {
    /* == Component version == */
    component: "yourComponent",
    bindings: {
        auth: 'auth',      // Inject auth loaded by resolve into component
        params: '$stateParams'
    }
}
}

Post a Comment for "Angular Route Resolve Calling A Service"