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

fix($rootScope): remove history event handler when app is torn down #9905

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/ng/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ function Browser(window, document, $log, $sniffer) {
return callback;
};

/**
* @private
* Remove popstate and hashchange handler from window.
*
* NOTE: this api is intended for use only by $rootScope.
*/
self.$$applicationDestroyed = function() {
jqLite(window).off('hashchange popstate', cacheStateAndFireUrlChange);
};

/**
* Checks whether the url has changed outside of Angular.
* Needs to be exported to be able to check for changes that have been done in sync,
Expand Down
7 changes: 6 additions & 1 deletion src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,12 @@ function $RootScopeProvider() {

this.$broadcast('$destroy');
this.$$destroyed = true;
if (this === $rootScope) return;

if (this === $rootScope) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (this === $rootScope) $browser.$$applicationDestroyed(); (or whatever you want to call it --- please precede it with 2 dollar signs though)

//Remove handlers attached to window when $rootScope is removed
$browser.$$applicationDestroyed();
return;
}

for (var eventName in this.$$listenerCount) {
decrementListenerCount(this, this.$$listenerCount[eventName], eventName);
Expand Down
1 change: 1 addition & 0 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ angular.mock.$Browser = function() {
return listener;
};

self.$$applicationDestroyed = angular.noop;
self.$$checkUrlChange = angular.noop;

self.cookieHash = {};
Expand Down