Configuring angularjs routes at runtime

I have a routing setup:

angular.module('MyApp', ['MyApp.controllers']). config(['$routeProvider', function($routeProvider) { $routeProvider.when('/foo', { templateUrl: '/fooTemplate', controller: 'FooCtrl' }); }] ); 

What I would like to do is prefix the path to '/ foo' at runtime, so depending on how my application is configured, the route might be / foo, maybe / some / arbitrary / path / foo, etc. d ..

Ideally, I could write something like:

 ... $routeProvider.when(prefix + '/foo', { templateUrl: '/fooTemplate', ... 

But I can’t figure out how to insert this prefix into my configuration method.

+4
source share
1 answer

It should be something like:

 .when('/:prefix/foo', {templateUrl: 'details.html', controller: DetailsCtrl}) 

Then prefix will be available in the controller via $routeParams . If you want it to handle the foo option without a prefix, just just put it in the routing and make sure it comes before the example above.

 .when('/foo', {templateUrl: 'details.html', controller: DetailsCtrl}) .when('/:prefix/foo', {templateUrl: 'details.html', controller: DetailsCtrl}) 
+3
source

Source: https://habr.com/ru/post/1488748/


All Articles