Scope $destroy() doesn't call when removing DOM element (major memory leaks)
I noticed something really problematic today, that causes major memory leaks: When removing the an element by using element.remove() it removes the element and trigger a '$destroy' event, but (and it's a big but) it only trigger it in the element's domain. What actually happens is that the scope doesn't receive this event and never run the $destroy callback function. If there is any cleanup on the '$destroy' event, it would not happen (and also the entire scope might be alive) .
You can see it in this Plunker: http://plnkr.co/edit/8MimMLTn6Wgyv6YK6CD9
I created two identical directives, both listen to element.on('$destroy') and $scope.$on('$destroy') events. The first one I removed with ng-if, which trigger both events. The second one I removed by removing the DOM element and it only trigger the element.on('$destroy') and not the scope destruction. This can cause major memory leaks when you work both with angular and jquery (jQlite that angular uses).
This works as expected.
Scopes aren't necessarily linked with one element, so you should be the one invoking Scope.$destroy in your code when needed.
ng-if does it for you, so do other directives like ng-repeat, ng-switch and ng-view
Nowhere in the core codebase are both events linked together. It's your responsibility to listen to one and trigger the other if your directives needs it, depending its own logic.
If you have further questions, check this section of our CONTRIBUTING.md If you have a feature request or another issue to report, feel free to do so on github.
I can't think of a reliable way to link scope's $destroy with element's $destroy
In the end, I believe it's better for us to leave that responsibility to the directives that modify the DOM, and know how to handle their scopes