AngularJS - use routeProvider “when” variables to

2019-02-05 19:44发布

So this is what I am trying to accomplish:

'use strict';

var app = angular.module('myModule', ['ngRoute']);

app.config(function($routeProvider) {
  $routeProvider
    .when('/', {
      redirectTo: '/pages'
    })
    .when('/pages/:pageName', {
      templateUrl: 'views/pages/'+pageName+'html',
      controller: 'MainController'
    });
});

Basically, I want to use the uri to determine which template is used. Currently I get an error that pageName is not defined which I understand. What would be a different way of doing this? Thanks!

2条回答
姐就是有狂的资本
2楼-- · 2019-02-05 20:00

templateUrl can be use as function with returning generated URL. We can manipulate url with passing argument which takes routeParams.

See the example.

.when('/:screenName/list',{
    templateUrl: function(params){
         return params.screenName +'/listUI'
    }
})

Hope this help.

查看更多
该账号已被封号
3楼-- · 2019-02-05 20:12

templateUrl can be a function accepting object of route parameters:

.when('/pages/:pageName', {
    templateUrl: function(params) {
        return 'views/pages/' + params.pageName + '.html';
    },
    controller: 'MainController'
});
查看更多
登录 后发表回答