diff --git a/src/ng/browser.js b/src/ng/browser.js index 37f3ab297505..ed85631fce5e 100644 --- a/src/ng/browser.js +++ b/src/ng/browser.js @@ -1,6 +1,11 @@ 'use strict'; /* global stripHash: true */ +function getHash(url) { + var index = url.indexOf('#'); + return index === -1 ? '' : url.substr(index + 1); +} + /** * ! This is a private undocumented service ! * @@ -191,8 +196,10 @@ function Browser(window, document, $log, $sniffer) { } if (replace) { location.replace(url); - } else { + } else if (!sameBase) { location.href = url; + } else { + location.hash = getHash(url); } } return self; diff --git a/src/ng/location.js b/src/ng/location.js index c315551006ac..517e69361cab 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -68,6 +68,10 @@ function stripHash(url) { return index == -1 ? url : url.substr(0, index); } +function trimEmptyHash(url) { + return url.replace(/#$/,''); +} + function stripFile(url) { return url.substr(0, stripHash(url).lastIndexOf('/') + 1); @@ -847,10 +851,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) { diff --git a/test/ng/browserSpecs.js b/test/ng/browserSpecs.js index 7153b1645fde..5ffaabc34cb1 100755 --- a/test/ng/browserSpecs.js +++ b/test/ng/browserSpecs.js @@ -1,5 +1,7 @@ 'use strict'; +/* global getHash:true, stripHash:true */ + var historyEntriesLength; var sniffer = {}; @@ -51,6 +53,12 @@ function MockWindow(options) { mockWindow.history.state = null; historyEntriesLength++; }, + get hash() { + return getHash(locationHref); + }, + set hash(value) { + locationHref = stripHash(locationHref) + '#' + value; + }, replace: function(url) { locationHref = url; mockWindow.history.state = null;