6

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?

Jakub
  • 20,262
  • 8
  • 63
  • 92
Trevor
  • 12,574
  • 12
  • 78
  • 99

4 Answers4

12

You can always use:

window.location 

or:

window.location.hash 

(if that's what you want).

Ben
  • 2,651
  • 27
  • 31
2

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.

Nelu
  • 13,698
  • 8
  • 71
  • 82
jsplaine
  • 602
  • 5
  • 16
  • 1
    Does html5Mode help with the solution, or is it just a better practice? If so, what's the advantage? – Trevor Dec 17 '13 at 22:30
  • 2
    [This stackoverflow](http://stackoverflow.com/a/16678065/1435916) answer explains what it does and why it may matter better than I could. – jsplaine Dec 17 '13 at 22:39
  • 1
    To better answer your question, the reason I used it was to get rid of the # appearing in the url bar. – jsplaine Dec 17 '13 at 23:08
1

You should listen to the $locationChangeSuccess event.

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

C.M.
  • 329
  • 3
  • 5
0

try concatenating the URI parts together

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

PS: doesn't work on Firefox :(

pkbyron
  • 153
  • 2
  • 9