ngOnDestroy

noun

say it: “EN-JEE on dih-STROY” /ˌɛn ˈdʒiː ɒn dɪˈstrɔɪ/

The Angular lifecycle method called just before a component is removed, used to clean up.

What is ngOnDestroy in Angular?

ngOnDestroy — The lifecycle method Angular calls just before it removes a component from the page. Use it to stop timers and anything else that would keep running after the component is gone.

export class Countdown implements OnDestroy {
  private readonly timer = setInterval(() => this.tick(), 1000);
  ngOnDestroy(): void {
    clearInterval(this.timer);
  }
  private tick(): void { /* ... */ }
}

In a sentence

  • “The countdown's timer is cleared in ngOnDestroy, so it stops when you leave the page.”

Level: Apprentice · More Angular terms at this level

Updated