By listening to this event, you can remove event listeners that might cause memory leaks. Listeners registered to scopes and elements are automatically cleaned up when they are destroyed, but if you registered a listener on a service, or registered a listener on a DOM node that isn't being deleted, you'll have to clean it up yourself or you risk introducing a memory leak.
Best Practice: Directives should clean up after themselves. You can use element.on('$destroy', …) or scope.$on('$destroy', …) to run a clean-up function when the directive is removed.
I have a
element.on "click", (event) -> inside my directive:
- When the directive is destroyed, are there any memory references to the
element.onto keep it from being garbage collected?
- Angular documentation states that I should use a handler to remove event listeners on the
$destroyemitted event. I was under the impression that
destroy()removed event listeners, is this not the case?