Skip to content

Instantly share code, notes, and snippets.

@samwx
Created August 17, 2018 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samwx/43ec4bcc3625f0051ff131a2e918291c to your computer and use it in GitHub Desktop.
Save samwx/43ec4bcc3625f0051ff131a2e918291c to your computer and use it in GitHub Desktop.
Deregistering $rootScope function listeners
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