Skip to content

Commit 365fd5d

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 angular#9897
1 parent 0f7bcfd commit 365fd5d

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/ng/browser.js

+10
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,16 @@ 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).off('hashchange popstate', cacheStateAndFireUrlChange);
304+
};
305+
296306
/**
297307
* Checks whether the url has changed outside of Angular.
298308
* Needs to be exported to be able to check for changes that have been done in sync,

src/ng/rootScope.js

+6-1
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

+1
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)