简体   繁体   English

如何获取当前ui-router状态的完整URL?

[英]How can I get the full url of the current ui-router state?

I'm using ui-router for AngularJS; 我正在为AngularJS使用ui-router; I'm handling the '$stateChangeSuccess' event, and trying to get the full path (after '#') for the current state. 我正在处理'$stateChangeSuccess'事件,并尝试获取当前状态的完整路径(在'#'之后)。 $location.hash() returns an empty string and $state.url returns only the portion of the path that applies to the nested state. $location.hash()返回一个空字符串, $state.url仅返回应用于嵌套状态的路径部分。

My full path #/a/b 我的完整路径#/a/b

$state.url == "/b"
$locadtion.hash() == ""

How can I get #/a/b or /a/b ? 我怎样才能获得#/a/b/a/b

You can always use: 你可以随时使用:

window.location 

or: 要么:

window.location.hash 

(if that's what you want). (如果那是你想要的)。

Have you tried $location.url()? 你试过$ location.url()吗? That should include the hash. 那应该包括哈希。 Actually, my test reveals that it doesn't grab the hash. 实际上,我的测试显示它没有抓住哈希。

$location docs $ location docs

As an aside, you may want to look into setting html5Mode in $locationProvider. 另外,您可能需要考虑在$ locationProvider中设置html5Mode。

You should listen to the $locationChangeSuccess event. 你应该听$locationChangeSuccess事件。

There you can use $location.path() or window.location.hash depending on your needs. 您可以根据需要使用$location.path()window.location.hash

try concatenating the URI parts together 尝试将URI部分连接在一起

window.location.origin + window.location.pathname + window.location.hash window.location.origin + window.location.pathname + window.location.hash

PS: doesn't work on Firefox :( PS:在Firefox上不起作用:(

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

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