Can I reclaim control of the URL hash-fragment fro

2019-02-11 21:47发布

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: http://my.domain.com#my-hash
  • is turned into http://my.domain.com/#/my-hash, and
  • $location.hash() will be empty.

And when html5Mode is enabled:

  • A given URL: http://my.domain.com#my-hash
  • is turned into http://my.domain.com/my-hash, 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: http://my.domain.com##my-hash
  • $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?

标签: angularjs url
1条回答
贪生不怕死
2楼-- · 2019-02-11 22:13

# 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) {
    $locationProvider.html5Mode(true).hashPrefix('!');
});

makes the difference.

查看更多
登录 后发表回答