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:
http://my.domain.com#my-hash
http://my.domain.com/#/my-hash
, and$location.hash()
will be empty.And when html5Mode
is enabled:
http://my.domain.com#my-hash
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:
http://my.domain.com##my-hash
$location.hash()
is finally equal to "my-hash"
, and$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 Change Url Example:$location. url('/User/'+userId); will set the current url to the new url example – www.example.com/User/98. You can call the function – changeUrl(userId) on button click.
The primary resource is identified by a Uniform Resource Identifier (URI), and the fragment identifier points to the subordinate resource. The fragment identifier introduced by a hash mark # is the optional last part of a URL for a document. It is typically used to identify a portion of that document.
Uses of HTML5 Mode in AngularJS html5Mode is a way to tell the browser that it needs to use HTML5 mode for URLs instead of the older HTTP protocol. This will allow browsers to use features like pushState , which is not supported by older browsers.
Fragment identifiers are not sent to the server. The hash fragment is used by the browser to link to elements within the same page.
#
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With