ui-router 可选参数,不带斜杠 [英] ui-router optional param without trailing slash
问题描述
所以这似乎是一个常见问题,但我还没有找到任何明确的答案.基本上我有一个状态:
So this seems like a common problem but I haven't found any definite answer. Basically I have a state:
.state('users', {
url: '/example/:id',
templateUrl: 'angular-views/example.html',
controller: 'ExampleCtrl'
})
我希望 id 是可选的.
I want id to be optional.
确定匹配
example/
example/1234
但是如果没有尾部斜杠,它就不匹配.
But it doesn't match without trailing slash.
example
我尝试了 $urlMatcherFactoryProvider.strictMode(false);
,但这似乎不适用于这种情况.我可以使用 example?param=1234
,但我更喜欢更简洁的版本.
I tried $urlMatcherFactoryProvider.strictMode(false);
, but that does not seem to work for this case. I could use example?param=1234
, but I prefer the cleaner version.
我真的需要定义第二个状态才能使其工作吗?
Do I really need to define a second state for this to work?
推荐答案
您可以通过在 params
对象中为它们提供默认值来定义可选的 URL 参数,如下所示.squash
将在链接中隐藏未定义的参数.
You can define optional URL parameters by giving them a default value in the params
object, like this. squash
will hide the parameter in links if it's not defined.
.state('users', {
url: '/example/:id',
templateUrl: 'angular-views/example.html',
controller: 'ExampleCtrl',
params: {
id: {
value: null,
squash: true
}
}
});
我在本地尝试了这个,无论尾部斜杠如何,它似乎都可以正常工作.
I tried this locally and it seems to work OK regardless of trailing slash.
这篇关于ui-router 可选参数,不带斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!