Angularjs $location.path() 无法定位到当前路由器

Angularjs $location.path() cannot locate to current router

我用angularjs和ui-router来写一页。而且我发现我的路由器在当前路由器中不工作。

例如:

当前路由器是/a/b$location.path()$location.path("/a/b")不工作

如果我定位到一个新路径,$location.path("/a/c"),它正在工作。

请就我的问题给我一些建议。谢谢。

如果当前url是

/a/b

UI-Router 的角度来看,这意味着 - 一些 状态 例如

.state("stateA", { 
  url: "/a/:id" 
  ...
}

和UI-Router,不会触发状态变化,当我们调用:

$location.path("/a/b")

因为这意味着 - NO 状态改变。另一方面,这个调用

$location.path("/a/c") // check the 'c' at the end

正在更改状态的 :id 参数,将导致状态重新加载。

我会说,强制状态更改的首选方法是使用服务 $state 及其方法 .go()

$state.go("stateA", params, options)

选项可以是:

{reload: true}

这将强制重新加载当前状态。检查文档

$state 服务

go(to, params, options)

...

Options object. The options are:

  • location - {boolean=true|string=} - 如果为 true 将更新位置栏中的 url,如果为 false 则不会。如果是字符串,必须是 "replace",这将更新 url 并替换上一条历史记录。
  • inherit - {boolean=true},如果为 true 将从当前 url.
  • 继承 url 参数
  • relative - {object=$state.$current},当使用相对路径(例如'^')转换时,定义相对于哪个状态。
  • notify - {boolean=true},如果为 true 将广播 $stateChangeStart 和 $stateChangeSuccess 事件。
  • reload (v0.2.5) - {boolean=false},如果为 true 将强制转换,即使状态或参数没有改变,也就是重新加载相同的状态。它与 reloadOnSearch 不同,因为当一切都相同(包括搜索参数)时您想要强制重新加载时会使用它。