Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
42
rated 0 times [  45] [ 3]  / answers: 1 / hits: 15345  / 11 Years ago, wed, september 4, 2013, 12:00:00

I'm trying to write my first web-app with Angular.



In the normal mode (html5Mode off), Angular forces the address's hash part to look like a path (adding a leading /), and encodes special characters - for example, it allows a single ? and # in the hash and replaces the others with %3F and %23.



Is there a way to turn this feature off? I don't want to use the $locationProvider / $routeProvider features - I want to parse the hash myself (In my case, the user's will enter some free text in the hash to search inside my website).



I read that the routeProvider cannot be configured to use regular expressions...



If htmlMode is turned on, then the address's hash part is not forced to look like a path (no leading /), but it still encodes special characters.



I'm aware that some browsers might encode/escape the special characters anyway, but if the user managed to enter some special characters in its address bar then I don't want to change it.



Thanks


More From » angularjs

 Answers
17

Not sure of the side effects of this, but it gets the job done. Note that it will disable all location manipulation from the angular app, even if intended.



angular.module('sample', [])
.config( ['$provide', function ($provide){
$provide.decorator('$browser', ['$delegate', function ($delegate) {
$delegate.onUrlChange = function () {};
$delegate.url = function () { return };
return $delegate;
}]);
}]);


ES6 variant:



angular.module('sample', [])
.config([$provide, $provide => {
$provide.decorator($browser, [$delegate, $delegate => {
$delegate.onUrlChange = () => { };
$delegate.url = () => ;

return $delegate;
}]);
}]);


Tested in Chrome 30, IE9, IE10.

Inspired by https://stackoverflow.com/a/16678065/369724


[#75903] Tuesday, September 3, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
joanneamiyaa

Total Points: 532
Total Questions: 127
Total Answers: 98

Location: Guam
Member since Tue, Nov 3, 2020
4 Years ago
joanneamiyaa questions
Thu, Apr 30, 20, 00:00, 4 Years ago
Thu, Feb 20, 20, 00:00, 4 Years ago
Mon, Oct 14, 19, 00:00, 5 Years ago
;