angularjs 路由可以有可选的参数值吗? [英] Can angularjs routes have optional parameter values?

查看:21
本文介绍了angularjs 路由可以有可选的参数值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用可选参数设置路由吗(相同的模板和控制器,但如果某些参数不存在,则应忽略它们?

Can I set a route with optional params (same template and controller, but some params should be ignored if they don't exist?

那么,与其写下面两条规则,不如只写一条?

So instead of writing the following two rules, have only one?

module.config(['$routeProvider', function($routeProvider) {
    $routeProvider.
     when('/users/', {templateUrl: 'template.tpl.html', controller: myCtrl}).            
     when('/users/:userId', {templateUrl: 'template.tpl.html', controller: myCtrl})
}]);

类似这样的东西([这个参数是可选的])

Something like this ([this param is optional])

when('/users[/:userId]', {templateUrl: 'template.tpl.html', controller: myCtrl})
//note: this previous doesn't work

我在他们的文档中找不到任何内容.

I couldn't find anything in their documentation.

推荐答案

看起来 Angular 现在已经支持这个了.

It looks like Angular has support for this now.

来自 $routeProvider.when(path, route):

path 可以包含可选的带问号的命名组 (:name?)

path can contain optional named groups with a question mark (:name?)

这篇关于angularjs 路由可以有可选的参数值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆