javascript - 如何在 AngularJS 路由中匹配可选和未确定的路由?

标签 javascript angularjs routes

在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/

相关文章:

javascript - 如何添加过渡效果以缓慢加载我的页面?

asp.net-mvc - 使用多值键创建 RouteValueDictionary

java - 使用 AngularJS 时 Java Web 框架的必要性

node.js - 在 Express 上伪造 req.host 值

authentication - 以 slim 为条件重定向整个组

javascript - AngularJS 中的数字格式

javascript - 如何提供动态更新的视频?

javascript - 将 json 字符串转换为适合 esri.geometry.Multipoint 的字符串

javascript - 如何使用 AngularJS 保护源代码的知识产权

javascript - 为什么两个$scopes同时更新,重复数据