是否有可能有一个状态参数,看起来像angular-ui-router的路径?

时间:2016-09-09 22:38:53

标签: angularjs regex angular-ui-router

以下是这两条路线的示例:

.state("test", {
  url: "/test/{path}",
  template: "<div ui-view></div>"
})
.state("test.child", {
  url: "/child",
  template: "<p>child</p>"
})

以下网址有效:

  • /test/thing将呈现test的观看次数,$stateParams.path呈现"thing"
  • /test/thing/other将呈现test的观看次数,$stateParams.path呈现"thing/other"
  • /test/thing/child将呈现test.child的观看次数,$stateParams.path呈现"thing"
  • /test/thing/other/child将呈现test.child的观看次数,$stateParams.path呈现"thing/other"

我已经尝试在path param上使用regexp,这样除了"/child"之外的任何字符串都会占用任何字符串,但这只会将我重定向到我的默认状态,因为没有匹配与正则表达式。

我目前的解决方法是使用其他分隔符,例如,而不是/作为我的路径参数(给我这些网址:/test/thing,other/child),并且我&#39;我现在要继续使用它,但我仍然想知道这种情况是否可行。

1 个答案:

答案 0 :(得分:1)

尝试:

app.config(function ($stateProvider) {
  $stateProvider
    .state("test-child", {
      url: "/test/{path:.*}/child",
      template: "<h1>CHILD's view</h1>"
    })
    .state("test", {
      url: "/test/{path:.*}",
      template: "<h1>TEST's view</h1>"
    })
});

plunker:FindString