在AngularJS路由中,我知道星号(*)可以匹配带斜杠的路径,问号(?)可以匹配可选路径,但是如何将它们组合起来?我想将 /admin/tasks
和 /admin/tasks/任意/path/with/slashes
匹配在一起。
我尝试过这个:
$routeProvider.when('/admin/tasks/:path*?', {
templateUrl: './templates/admin/task.html',
controller: 'AdminTaskController'
})
它可以匹配/admin/tasks/
和/admin/tasks/任意/path/with/slashes
,但不能匹配/admin/tasks
。这很奇怪,因为 doc说:
Route path (matched against
$location.path
). If$location.path
contains redundant trailing slash or is missing one, the route will still match and the$location.path
will be updated to add or drop the trailing slash to exactly match the route definition.
为什么路由无法匹配没有尾部斜杠的 URL?
此外,在文档中,
path
can contain optional named groups with a question mark: e.g.:name?
.
但实际上单独使用 ?
无法匹配带斜杠的路径。
最佳答案
我通过重复解决了它。这并不理想,但它有效:
.when('/system/:path?',{
templateUrl:'assets/templates/pages/system.html',
controller: "SystemController",
controllerAs: "systemCtrl"
})
.when('/system/:path*',{
templateUrl:'assets/templates/pages/system.html',
controller: "SystemController",
controllerAs: "systemCtrl"
});
现在,当人们输入 http://localhost/system/files/15/delete
时,它会识别它,如果他们只是转到 http://localhost/system
>
关于javascript - 如何在 AngularJS 路由中匹配可选和未确定的路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28188293/