2

I'd like to add Angular resolve dynamically after it was defined by the app. Imagine, my app is defined with routes like this:

app.config(['$routeProvider', function ($routeProvider) {
  $routeProvider
    .when("/", {templateUrl: "partials/home.html", controller: "PageCtrl"   })
    .when("/services", {templateUrl: "partials/services.html", controller: "PageCtrl"   })
}]);

Then later I want to add/edit/override specific route(s) to resolve a promise:

app.config(['$routeProvider', function ($routeProvider) {
  $routeProvider
    .when("/services", {templateUrl: "partials/services.html", controller: "PageCtrl",  ,resolve:{getData: function(myService){return myService.getOffer();}}    })
}]);

How do I do this? The key here is adding after it was defined/configured. So this code will run at some point in the future.

1 Answer 1

1

It is

app.run(function ($route) {
  var route = $route.routes['/...'];
  route.resolve = route.resolve || {};
  route.resolve.someResolver = ...;
});
Sign up to request clarification or add additional context in comments.

8 Comments

This does nicely work for the page I add resolve to (eg: $route.routes['/services'];)... But when I go to other pages I get Angular Error: [$injector:unpr]
Since you use the same controller for all routes, I guess you're injecting a resolver there on the route where you didn't define this resolver.
I added route.controller = "PageCtrl"; inside - but that did not fix. Is this what you refer to?
Please, post your controller code. I assume that you're injecting a resolver into the controller on the route which doesn't have this resolver.
I understood. Thank you. I indeed was injecting a undefined resolver into the controller
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.