javascript - 使 AngularJS 路由命名组非贪婪

标签 javascript angularjs routes greedy

给定以下路线:

$routeProvider.when('/users/:userId-:userEncodedName', {
    ...
})

当点击 URL /users/42-johndoe 时,$routeParams 会按预期进行初始化:

$routeParams.userId // is 42
$routeParams.userEncodedName // is johndoe

但是当点击 URL /users/42-john-doe 时,$routeParam 被初始化如下:

$routeParams.userId // is 42-john
$routeParams.userEncodedName // is doe

有什么方法可以使命名组成为非贪婪的,即获得以下$routeParams:

$routeParams.userId // is 42
$routeParams.userEncodedName // is john-doe

?

最佳答案

你可以改变路径

来自

$routeProvider.when('/users/:userId-:userEncodedName', {});

$routeProvider.when('/users/:userId*-:userEncodedName', {})

AngularJS Documentation 中所述关于 $routeProviderspath 属性:

path can contain named groups starting with a colon and ending with a star: e.g.:name*. All characters are eagerly stored in $routeParams under the given name when the route matches.

关于javascript - 使 AngularJS 路由命名组非贪婪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24067884/

相关文章:

javascript - 我如何获得当前处于警报弹出窗口中的结果,以在 html 中输入

javascript - 如何在 Ubuntu 上安装 JSLint?

javascript - 关于AngularJS中数据绑定(bind)的疑问

node.js - 通过请求正文将 Sails.js 路由绑定(bind)到模型蓝图操作

java - 从javascript访问java对象的成员函数

javascript - 'Enter key' 不会在 Firefox 中提交表单,但在 Chrome 中会,为什么?

javascript - 更改单页 Angular 应用程序中的 View

javascript - 使用 ng-bind-html 时 Angular 选项卡会中断

c# - WPF:点击用户控件背景不触发

javascript - 路由整个文件夹及其内容