Trevor
Trevor

Reputation: 13437

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

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?

Upvotes: 6

Views: 19884

Answers (4)

C.M.
C.M.

Reputation: 437

You should listen to the $locationChangeSuccess event.

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

Upvotes: 1

pkbyron
pkbyron

Reputation: 163

try concatenating the URI parts together

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

PS: doesn't work on Firefox :(

Upvotes: 0

jsplaine
jsplaine

Reputation: 621

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.

Upvotes: 2

Ben
Ben

Reputation: 2651

You can always use:

window.location 

or:

window.location.hash 

(if that's what you want).

Upvotes: 12

Related Questions