Overview - ngOnDestroy for cleanup
What is it?
ngOnDestroy is a special method in Angular components and directives that runs just before the component or directive is removed from the screen. It is used to clean up resources like timers, subscriptions, or event listeners to prevent memory leaks. Think of it as a tidy-up step that Angular calls automatically. Without it, leftover tasks could slow down or break your app.
Why it matters
Without ngOnDestroy, apps can keep using memory and resources even after parts of the interface disappear. This causes apps to become slow or crash over time, especially in complex or long-running applications. ngOnDestroy helps keep apps healthy by making sure everything is cleaned up properly when no longer needed.
Where it fits
Before learning ngOnDestroy, you should understand Angular components, lifecycle hooks, and how subscriptions or event listeners work. After mastering ngOnDestroy, you can explore advanced Angular lifecycle management, RxJS subscription handling, and performance optimization techniques.