Can I reclaim control of the URL hash-fragment from AngularJS?


I'm using AngularJS, and would like to process a given #hash-fragment in the address-bar. However, —and this is key— I'll not be using the hash-fragment to encode a path. But it seems AngularJS insists on interpreting it that way.

For example, when html5Mode is disabled:

  • A given URL:
  • is turned into, and
  • $location.hash() will be empty.

And when html5Mode is enabled:

  • A given URL:
  • is turned into, and
  • $location.hash() will still be empty.

AngularJS must be thinking: "oh, you have html5, let me remove that hash for you". How considerate…

The only way to get anything from $location.hash() is when the URL has a double hash:

  • For a given URL:
  • $location.hash() is finally equal to "my-hash", and
  • if also $locationProvider.html5Mode(true) one of the hashes is stripped from the URL.

But I really need a simple single hash character to remain unprocessed. Is this possible?

Problem courtesy of: mhelvens


# seems to work well enough on the angular api docs page. So with a little snooping and testing I found that

app.config(function($locationProvider) {

makes the difference.

Solution courtesy of: towr


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.