Destruction and cleanup
Learn Destruction and cleanup (Module 9 · Component Lifecycle, Angular) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.
Cleaning up after yourself • When a component is removed (route change, @if turns false) Angular calls ngOnDestroy() once. Like Java's close() in try-with-resources, this is where you release things: stop timers, unsubscribe, close sockets. • If you forget, the timer or subscription keeps running in memory: a memory leak , and sometimes code keeps updating a component that no longer exists.