我有两条路线通向相同的视图和控制器(即我只是传入一个 id 以在 $routeParams 中访问并在其上执行控制器逻辑):
$routeProvider
.when('/about',
{
controller: 'AboutController',
controllerAs: 'vm',
templateUrl: 'about.html'
})
.when('/about/:id',
{
controller: 'AboutController',
controllerAs: 'vm',
templateUrl: 'about.html'
});
Run Code Online (Sandbox Code Playgroud)
这感觉非常重复。有没有速记之类的?
$routeProvider
.when(['/about', '/about/:id'],
{
controller: 'AboutController',
controllerAs: 'vm',
templateUrl: 'about.html'
})
Run Code Online (Sandbox Code Playgroud)
从$routeProvider源代码来看,我认为这是不可能的。this.when方法接受两个参数,path并且route。对于多个路径,this.when应该接受路径数组作为参数,或者从单个字符串中提取多个路径。我在这个方法中没有看到这两个。
this.when = function(path, route) {
//copy original route object to preserve params inherited from proto chain
var routeCopy = angular.copy(route);
if (angular.isUndefined(routeCopy.reloadOnSearch)) {
routeCopy.reloadOnSearch = true;
}
if (angular.isUndefined(routeCopy.caseInsensitiveMatch)) {
routeCopy.caseInsensitiveMatch = this.caseInsensitiveMatch;
}
routes[path] = angular.extend(
routeCopy,
path && pathRegExp(path, routeCopy)
);
// create redirection for trailing slashes
if (path) {
var redirectPath = (path[path.length - 1] === '/')
? path.substr(0, path.length - 1)
: path + '/';
routes[redirectPath] = angular.extend(
{redirectTo: path},
pathRegExp(redirectPath, routeCopy)
);
}
return this;
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
937 次 |
| 最近记录: |