本文介绍了Angular ui.router,创建一个可选的第一个路径参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Angular 相对较新,但我似乎找不到任何 url 别名或完全可选的路径参数(使用 ui.router 的 $stateProvider.

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,但这仍然需要 var1 的空值 (//var2)
  • /:var2?var1/:var2/:var1?,但它们都不是在上下文中非常理想,因为它们重新排列了路径.
  • /:var1/:var2 AND /:var2 作为单独的状态,但我可能需要更密切地管理状态更改并根据哪些参数进行选择可用的.
  • /:var1/:var2, but this still requires an empty value for var1 (//var2)
  • /:var2?var1 or /:var2/:var1?, but neither of these areideal 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', {网址:'/:var1/:var2'}).state('app.view2', {网址:'/:var2'});

推荐答案

也许这行得通

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

squash 配置当当前参数值与默认值相同时,如何在 URL 中表示默认参数值

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

这篇关于Angular ui.router,创建一个可选的第一个路径参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 22:01