0

In AngularJS route set up, I could use the "resolveRedirectTo" facility to calculate a value using an injected service, which I could then use in the redirect URL, e.g.

myModule.config(['$routeProvider', function($routeProvider) { $routeProvider
  .when( '/user/:userId', {template:'<my-component>'} )
  .otherwise( {resolveRedirectTo: ['MyService', function(MyService){
        return '/user/' + MyService.getDefaultUserID();}]
  });
}]);

How can I do the same in Angular 2 (or Angular 4, etc)? Specifically, how to invoke an injected service so that I can dynamically create the URL to redirect to.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
user292701
  • 170
  • 1
  • 1
  • 10
  • The [Angular routing documentation](https://angular.io/guide/router) is very good. I'm not familiar with angularjs so not 100% sure on what that code does but it looks like a [Route Guard](https://angular.io/guide/router#milestone-5-route-guards) might do what you want it to. [This question](https://stackoverflow.com/questions/39002288/angular-2-authguard-service-with-redirect) deals with how to navigate to any other page from within a route guard – 0mpurdy Aug 04 '17 at 22:50
  • The Angular docs do not address this feature from AngularJS. This very compact notation allowed you to specify, directly in the route definition, that you wanted to resolve a service request to then use that info to create the redirect URL string. I see nothing close to documenting that scenario in Angular, and they apparently did not directly port that feature from AngularJS. – user292701 Aug 04 '17 at 23:51
  • you can add a resolve or guard to the wildcard route like any other route, and guards and resolvers can both inject whatever you need. – bryan60 Aug 05 '17 at 02:25

0 Answers0