简体   繁体   English

带角度ui-router的多个可选参数

[英]Multiple optional parameters with angular ui-router

I am trying to use multiple optional parameters with ui-router but it does not seem to work. 我试图使用ui-router多个可选参数但它似乎不起作用。 Below are the tests i did: 以下是我做的测试:

Single parameter is OK 单个参数可以

state.url       url called     state param values

/page/:x        /page/         $stateParams.x == ""
/page/:x        /page/2        $stateParams.x == "2"

One optional parameter is OK 一个可选参数是OK

/page/:x?       /page/         $stateParams.x == ""
/page/:x?       /page/2        $stateParams.x == "2"

Two parameters are OK (except the ugly double slashes in first case, /page and /page/ turn into /page// . But since the parameters are not optional that's OK) 两个参数都可以(除了第一种情况下丑陋的双斜线, /page/page/转到/page// 。但是因为参数不是可选的,所以没问题)

/page/:x/:y     /page//        $stateParams.x == "" && $stateParams.y == ""
/page/:x/:y     /page/2        $stateParams.x == "" && $stateParams.y == ""
/page/:x/:y     /page/2/       $stateParams.x == "2" && $stateParams.y == ""
/page/:x/:y     /page/2/3      $stateParams.x == "2" && $stateParams.y == "3"

Two optional parameters behaves strange. 两个可选参数表现很奇怪。 Second parameters is always undefined and it cannot solve first parameter when I specify the second one. 第二个参数总是未定义的,当我指定第二个参数时,它不能解决第一个参数。

/page/:x?/:y?   /page/         $stateParams.x == "" && $stateParams.y == undefined
/page/:x?/:y?   /page/2        $stateParams.x == "2" && $stateParams.y == undefined
/page/:x?/:y?   /page/2/       $stateParams.x == "" && $stateParams.y == undefined
/page/:x?/:y?   /page/2/3      $stateParams.x == "" && $stateParams.y == undefined

UI-Router optional parameter are not specified by modifying the URL. 未通过修改URL指定UI-Router可选参数。 Rather, they are specified using the params object in the state definition. 相反,它们是使用状态定义中的params对象指定的。

The declaration url: '/page/:x?/:y?' 声明url: '/page/:x?/:y?' does not mark x and y as optional parameters. 不会将x和y标记为可选参数。 Instead, the question mark is used to separate the URL and Query Param portion of the URL. 相反,问号用于分隔URL的URL和Query Param部分。

Read the description text in the UrlMatcher docs and the $stateProvider.state docs for url , then for params . 阅读UrlMatcher文档中的描述文本和url$ stateProvider.state文档,然后阅读params

You will end up with optional params configured like so: 你最终会得到如下配置的可选参数:

$stateProvider.state('page', {
  url: '/page/:x/:y',
  params: { 
    x: 5,  // default value of x is 5
    y: 100 // default value of y is 100
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM