给定以下路线:
$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 中所述关于 $routeProviders
,path
属性:
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/