简体   繁体   中英

Angular - Use routes parameter in route setup

I'm trying to create a BaseController which my controllers can extend to handle the history action for all models. Currently (for the other routes), I set it up like so:

$routeProvider.when('/', {
  redirectTo: '/static/campaigns'
}).when('/static/campaigns/new', {
  templateUrl: 'campaigns/new.html',
  controller: 'CampaignNewController'
}).....

What I want to do for the history routes would be a general route which would cover all models, but I'm not sure if there's a way to use the route parameters within the setup like below.

when('/static/history/:model_name', {
  templateUrl: model_name + '/history.html',
  controller: model_name + 'Controller'
})

If you look at documentation : https://docs.angularjs.org/api/ngRoute/provider/$routeProvider#when you may notice that, templateurl can be a string or a function. Perhaps if you created a function on the right hand side of templateUrl and injected $routeParams and read the value of model_name and returned a string with model_name, it might just work.

You wont be able to do the same for controller; however, you will have to create a service, that takes model_name as input and does the same logic as multiple controllers would.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM