Skip to content

Commit 1324db4

Browse files
committed
fix($browser): detect changes to the browser url that happened in sync
Closes angular#6976.
1 parent 5f90340 commit 1324db4

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

src/ng/browser.js

+7
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,13 @@ function Browser(window, document, $log, $sniffer) {
234234
return callback;
235235
};
236236

237+
/**
238+
* Checks whether the url has changed outside of Angular.
239+
* Needs to be exported to be able to check for changes that have been done in sync,
240+
* as hashchange/popstate events fire in async.
241+
*/
242+
self.$$checkUrlChangedOutsideAngular = fireUrlChange;
243+
237244
//////////////////////////////////////////////////////////////
238245
// Misc API
239246
//////////////////////////////////////////////////////////////

src/ng/rootScope.js

+2
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,8 @@ function $RootScopeProvider(){
687687
logIdx, logMsg, asyncTask;
688688

689689
beginPhase('$digest');
690+
// Check for changes to browser url that happened in sync with the call to $digest
691+
$browser.$$checkUrlChangedOutsideAngular();
690692

691693
lastDirtyWatch = null;
692694

src/ngMock/angular-mocks.js

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ angular.mock.$Browser = function() {
5656
return listener;
5757
};
5858

59+
self.$$checkUrlChangedOutsideAngular = angular.noop;
60+
5961
self.cookieHash = {};
6062
self.lastCookieHash = {};
6163
self.deferredFns = [];

test/ng/locationSpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,28 @@ describe('$location', function() {
769769
});
770770

771771

772+
it('should update location when location changed outside of Angular', function() {
773+
module(function($windowProvider, $locationProvider, $browserProvider) {
774+
$locationProvider.html5Mode(true);
775+
$browserProvider.$get = function($document, $window, $log, $sniffer) {
776+
var b = new Browser($window, $document, $log, $sniffer);
777+
b.pollFns = [];
778+
return b;
779+
};
780+
});
781+
inject(function($rootScope, $browser, $location, $sniffer){
782+
if ($sniffer.history) {
783+
window.history.replaceState(null, '', '/hello');
784+
// Verify that infinite digest reported in #6976 no longer occurs
785+
expect(function() {
786+
$rootScope.$digest();
787+
}).not.toThrow();
788+
expect($location.path()).toBe('/hello');
789+
}
790+
});
791+
});
792+
793+
772794
it('should rewrite when hashbang url given', function() {
773795
initService(true, '!', true);
774796
inject(

0 commit comments

Comments
 (0)