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
How can I get the full url of the current ui-router state?
提问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.url
returns 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/b
or /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() 吗? 这应该包括哈希。实际上,我的测试表明它没有获取哈希值。
As an aside, you may want to look into setting html5Mode in $locationProvider.
顺便说一句,您可能需要研究在 $locationProvider 中设置 html5Mode。
回答by C.M.
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
取决于您的需要。
回答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 :(