mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-17 12:15:56 +08:00
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
This commit is contained in:
@@ -68,6 +68,10 @@ function stripHash(url) {
|
||||
return index == -1 ? url : url.substr(0, index);
|
||||
}
|
||||
|
||||
function trimEmptyHash(url) {
|
||||
return url.replace(/(#.+)|#$/, '$1');
|
||||
}
|
||||
|
||||
|
||||
function stripFile(url) {
|
||||
return url.substr(0, stripHash(url).lastIndexOf('/') + 1);
|
||||
@@ -903,10 +907,11 @@ function $LocationProvider() {
|
||||
|
||||
// update browser
|
||||
$rootScope.$watch(function $locationWatch() {
|
||||
var oldUrl = $browser.url();
|
||||
var oldUrl = trimEmptyHash($browser.url());
|
||||
var newUrl = trimEmptyHash($location.absUrl());
|
||||
var oldState = $browser.state();
|
||||
var currentReplace = $location.$$replace;
|
||||
var urlOrStateChanged = oldUrl !== $location.absUrl() ||
|
||||
var urlOrStateChanged = oldUrl !== newUrl ||
|
||||
($location.$$html5 && $sniffer.history && oldState !== $location.$$state);
|
||||
|
||||
if (initializing || urlOrStateChanged) {
|
||||
|
||||
@@ -647,6 +647,18 @@ describe('$location', function() {
|
||||
};
|
||||
}
|
||||
|
||||
describe('location watch', function() {
|
||||
beforeEach(initService({supportHistory: true}));
|
||||
beforeEach(inject(initBrowser({url:'http://new.com/a/b#'})));
|
||||
|
||||
it('should not update browser if only the empty hash fragment is cleared by updating the search', inject(function($rootScope, $browser, $location) {
|
||||
$browser.url('http://new.com/a/b#');
|
||||
var $browserUrl = spyOnlyCallsWithArgs($browser, 'url').andCallThrough();
|
||||
$rootScope.$digest();
|
||||
expect($browserUrl).not.toHaveBeenCalled();
|
||||
}));
|
||||
});
|
||||
|
||||
describe('wiring', function() {
|
||||
|
||||
beforeEach(initService({html5Mode:false,hashPrefix: '!',supportHistory: true}));
|
||||
|
||||
Reference in New Issue
Block a user