ui-router optional param without trailing slash

走远了吗. 提交于 2019-12-21 06:58:02

问题


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'
    })

I want id to be optional.

Sure it matches

example/
example/1234

But it doesn't match without trailing slash.

example

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?


回答1:


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.



来源:https://stackoverflow.com/questions/28840896/ui-router-optional-param-without-trailing-slash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!