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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20645783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:56:30  来源:igfitidea点击:

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

javascriptangularjsangular-ui-router

提问by Trevor

I'm using ui-router for AngularJS; I'm handling the '$stateChangeSuccess'event, and trying to get the full path (after '#') for the current state. $location.hash()returns an empty string and $state.urlreturns only the portion of the path that applies to the nested state.

我正在为 AngularJS 使用 ui-router;我正在处理该'$stateChangeSuccess'事件,并尝试获取当前状态的完整路径(在“#”之后)。 $location.hash()返回一个空字符串并$state.url仅返回适用于嵌套状态的路径部分。

My full path #/a/b

我的完整路径 #/a/b

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

How can I get #/a/bor /a/b?

我怎样才能得到#/a/b/a/b

回答by Ben

You can always use:

您可以随时使用:

window.location 

or:

或者:

window.location.hash 

(if that's what you want).

(如果那是你想要的)。

回答by jsplaine

Have you tried $location.url()? That should include the hash.Actually, my test reveals that it doesn't grab the hash.

你试过 $location.url() 吗? 这应该包括哈希。实际上,我的测试表明它没有获取哈希值。

$location docs

$location 文档

As an aside, you may want to look into setting html5Mode in $locationProvider.

顺便说一句,您可能需要研究在 $locationProvider 中设置 html5Mode。

回答by C.M.

You should listen to the $locationChangeSuccessevent.

你应该听听这个$locationChangeSuccess事件。

There you can use $location.path()or window.location.hashdepending on your needs.

在那里您可以使用$location.path()window.location.hash取决于您的需要。

回答by pkbyron

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 :(