最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

My full path #/a/b

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

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

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.

My full path #/a/b

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

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

Share Improve this question edited Dec 17, 2013 at 22:17 Jakub 20.5k8 gold badges66 silver badges93 bronze badges asked Dec 17, 2013 at 22:11 TrevorTrevor 13.5k13 gold badges81 silver badges103 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 12

You can always use:

window.location 

or:

window.location.hash 

(if that's what you want).

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

$location docs

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

You should listen to the $locationChangeSuccess event.

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

try concatenating the URI parts together

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

PS: doesn't work on Firefox :(

发布评论

评论列表(0)

  1. 暂无评论