gpt4 book ai didi

AngularJS $routeProvider 是否有一种具有相同设置的多个路由的速记方法?

转载 作者:行者123 更新时间:2023-12-01 00:42:07 27 4
gpt4 key购买 nike

我有两条路线通向相同的 View 和 Controller (即我只是传入一个 id 以在 $routeParams 中访问并在其上执行 Controller 逻辑):

$routeProvider
.when('/about',
{
controller: 'AboutController',
controllerAs: 'vm',
templateUrl: 'about.html'
})
.when('/about/:id',
{
controller: 'AboutController',
controllerAs: 'vm',
templateUrl: 'about.html'
});

这感觉非常重复。有没有速记之类的?
$routeProvider
.when(['/about', '/about/:id'],
{
controller: 'AboutController',
controllerAs: 'vm',
templateUrl: 'about.html'
})

最佳答案

From $routeProvider source code ,在我看来这是不可能的。 this.when方法接受两个参数,pathroute .对于多路径,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;
};

关于AngularJS $routeProvider 是否有一种具有相同设置的多个路由的速记方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36879968/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com