我试图设置我的路由这样
... url: '/view/:inBoxId?' ...
但是Angular会抛出这个错误:
Error: Invalid parameter name '' in pattern '/view/:inBoxId?'
所以基本上我不得不设置两个不同的状态:
state('view',{ url: '/view/:inBoxId',templateUrl: 'templates/view.html',controller: 'viewCtrl' }). state('view_root',{ url: '/view',controller: 'viewCtrl' })
有没有办法把这些状态合并成一个?
有一个可选的参数 – 按照你的说法声明 – 但不要通过它.这是一个
example.所有这些都可以使用一个状态(无根)或两个(根和细节),你喜欢.
问题中提到的定义已经准备好处理这些状态调用:
// href <a href="#/view/1">/view/1</a> - id is passed<br /> <a href="#/view/"> /view/ </a> - is is missing - OPTIONAL like <br /> <a href="#/view"> /view </a> - url matching the view_root // ui-sref <a ui-sref="view({inBoxId:2})"> - id is passed<br /> <a ui-sref="view"> - is is missing - OPTIONAL like <a ui-sref="view({inBoxId:null})"> - id is explicit NULL <br /> <a ui-sref="view_root()"> - url matching the view_root
我们不必使用将参数标记为可选.只有这两个网址必须是唯一的(例如/ view /:id vs / view – 其中第二个没有尾随/)