Angular ui.router, Creating an Optional First Path Param

不打扰是莪最后的温柔 提交于 2019-12-30 11:39:10

问题


Relatively new to working with Angular, but I couldn't seem to find any url aliasing or fully optional path params (something like {/var1}/:var2 when using ui.router's $stateProvider.

I'm looking to have the following both be valid paths, ideally on the same state declaration so that I can call state changes using one or both params.

/var1/var2
/var2

Naturally, I can use matches like the following, but none of these are true solutions.

  • /:var1/:var2, but this still requires an empty value for var1 (//var2)
  • /:var2?var1 or /:var2/:var1?, but neither of these are ideal in context as they rearrange the path.
  • /:var1/:var2 AND /:var2 as separate states, but I'd likely need to manage state changes more closely and select based on which params are available.

Any info to point me in the right direction would be greatly appreciated.

And a quick stripped down sample for context.

$stateProvider .state('app.view', { url : '/:var1/:var2' }).state('app.view2', { url : '/:var2' });


回答1:


Maybe this would work

.state('app.view', {
    url: '/:var1/:var2',
    templateUrl: 'yourTpl.html',
    controller: 'YourCtrl',
    params: {
        var1: {squash: true, value: null}
    }
})

squash configures how a default parameter value is represented in the URL when the current parameter value is the same as the default value



来源:https://stackoverflow.com/questions/38930529/angular-ui-router-creating-an-optional-first-path-param

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