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

Commit cb3c27b

Browse files
committed
fix($rootScope): remove history event handler when app is torn down
Remember the popstate and hashchange handler registered with window when the application bootstraps, and remove it when the application is torn down Closes #9897
1 parent 0f7bcfd commit cb3c27b

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/ng/browser.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,17 @@ function Browser(window, document, $log, $sniffer) {
293293
return callback;
294294
};
295295

296+
/**
297+
* @private
298+
* Remove popstate and hashchange handler from window.
299+
*
300+
* NOTE: this api is intended for use only by $rootScope.
301+
*/
302+
self.$$applicationDestroyed = function() {
303+
jqLite(window).unbind('popstate', cacheStateAndFireUrlChange);
304+
jqLite(window).unbind('hashchange', cacheStateAndFireUrlChange);
305+
};
306+
296307
/**
297308
* Checks whether the url has changed outside of Angular.
298309
* Needs to be exported to be able to check for changes that have been done in sync,

src/ng/rootScope.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,12 @@ function $RootScopeProvider() {
861861

862862
this.$broadcast('$destroy');
863863
this.$$destroyed = true;
864-
if (this === $rootScope) return;
864+
865+
if (this === $rootScope) {
866+
//Remove handlers attached to window when $rootScope is removed
867+
$browser.$$applicationDestroyed();
868+
return;
869+
}
865870

866871
for (var eventName in this.$$listenerCount) {
867872
decrementListenerCount(this, this.$$listenerCount[eventName], eventName);

src/ngMock/angular-mocks.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ angular.mock.$Browser = function() {
5757
return listener;
5858
};
5959

60+
self.$$applicationDestroyed = angular.noop;
6061
self.$$checkUrlChange = angular.noop;
6162

6263
self.cookieHash = {};

0 commit comments

Comments
 (0)