Created
August 17, 2018 13:07
-
-
Save samwx/43ec4bcc3625f0051ff131a2e918291c to your computer and use it in GitHub Desktop.
Deregistering $rootScope function listeners
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyController { | |
constructor($scope, $rootScope) { | |
/* | |
Every $rootScope.$on returns a deregistration function that remove listerner and avoid duplications | |
https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$on | |
*/ | |
const deregisterDropdownOpenEvent = $rootScope.$on('DropdownOpened', () => console.log('dispatch event')) | |
/* Deregister function on scope $destroy event (when user leave the page, for example) */ | |
$scope.$on('$destroy', () => deregisterDropdownOpenEvent()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment