Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 55ba752

Browse files
fix($location): strip off empty hash segments when comparing
The url is the same whether or not there is an empty `#` marker at the end. This prevents unwanted calls to update the browser, since the browser is automatically applying an empty hash if necessary to prevent page reloads. Closes #9635
1 parent 900bb66 commit 55ba752

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/ng/location.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ function stripHash(url) {
6868
return index == -1 ? url : url.substr(0, index);
6969
}
7070

71+
function trimEmptyHash(url) {
72+
return url.replace(/#$/,'');
73+
}
74+
7175

7276
function stripFile(url) {
7377
return url.substr(0, stripHash(url).lastIndexOf('/') + 1);
@@ -847,10 +851,11 @@ function $LocationProvider() {
847851

848852
// update browser
849853
$rootScope.$watch(function $locationWatch() {
850-
var oldUrl = $browser.url();
854+
var oldUrl = trimEmptyHash($browser.url());
855+
var newUrl = trimEmptyHash($location.absUrl());
851856
var oldState = $browser.state();
852857
var currentReplace = $location.$$replace;
853-
var urlOrStateChanged = oldUrl !== $location.absUrl() ||
858+
var urlOrStateChanged = oldUrl !== newUrl ||
854859
($location.$$html5 && $sniffer.history && oldState !== $location.$$state);
855860

856861
if (initializing || urlOrStateChanged) {

test/ng/locationSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,18 @@ describe('$location', function() {
639639
};
640640
}
641641

642+
describe('location watch', function() {
643+
beforeEach(initService({supportHistory: true}));
644+
beforeEach(inject(initBrowser({url:'http://new.com/a/b#'})));
645+
646+
it('should not update browser if only the empty hash fragment is cleared by updating the search', inject(function($rootScope, $browser, $location) {
647+
$browser.url('http://new.com/a/b#');
648+
var $browserUrl = spyOnlyCallsWithArgs($browser, 'url').andCallThrough();
649+
$rootScope.$digest();
650+
expect($browserUrl).not.toHaveBeenCalled();
651+
}));
652+
});
653+
642654
describe('wiring', function() {
643655

644656
beforeEach(initService({html5Mode:false,hashPrefix: '!',supportHistory: true}));

0 commit comments

Comments
 (0)