0
0
Angularframework~10 mins

ngOnDestroy for cleanup in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ngOnDestroy for cleanup
Component Initialized
Component Renders
Component in Use
Component Destroy Triggered
ngOnDestroy Called
Cleanup Tasks Executed
Component Removed from DOM
This flow shows how Angular calls ngOnDestroy when a component is about to be removed, allowing cleanup like unsubscribing or stopping timers.
Execution Sample
Angular
import { Component, OnDestroy } from '@angular/core';

@Component({ selector: 'app-demo', template: '<p>Demo</p>' })
export class DemoComponent implements OnDestroy {
  intervalId = setInterval(() => console.log('tick'), 1000);
  ngOnDestroy() {
    clearInterval(this.intervalId);
  }
}
This component starts a timer on creation and stops it in ngOnDestroy when the component is removed.
Execution Table
StepActionState BeforeState AfterEffect
1Component createdintervalId: undefinedintervalId: timer runningTimer starts logging 'tick' every second
2Component in useintervalId: timer runningintervalId: timer runningTimer continues running
3Component removal triggeredintervalId: timer runningintervalId: timer runningPreparing to destroy component
4ngOnDestroy calledintervalId: timer runningintervalId: clearedTimer stopped, no more 'tick' logs
5Component removedintervalId: clearedComponent goneCleanup complete, no memory leaks
💡 Component is removed, ngOnDestroy cleanup stops the timer to prevent resource leaks
Variable Tracker
VariableStartAfter Step 1After Step 4Final
intervalIdundefinedtimer runningclearedcleared
Key Moments - 3 Insights
Why do we clear the interval in ngOnDestroy?
Because the timer keeps running even after the component is removed, causing memory leaks or unwanted behavior. The execution_table row 4 shows ngOnDestroy stopping the timer.
When exactly is ngOnDestroy called?
It is called right before Angular removes the component from the DOM, as shown in execution_table step 4.
What happens if we don't implement ngOnDestroy for cleanup?
Resources like timers or subscriptions keep running, which can slow down the app or cause bugs. The execution_table shows the timer running before cleanup and stopping after ngOnDestroy.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of intervalId after step 1?
AUndefined
BTimer cleared
CTimer running
DComponent removed
💡 Hint
Check the 'State After' column in row 1 of execution_table
At which step does ngOnDestroy stop the timer?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for 'ngOnDestroy called' in the 'Action' column in execution_table
If we remove the clearInterval call, what would happen to intervalId after component removal?
AIt would be cleared automatically
BIt would keep running causing memory leaks
CIt would throw an error
DIt would stop after some time
💡 Hint
Refer to key_moments explanation about cleanup necessity
Concept Snapshot
ngOnDestroy is a lifecycle hook Angular calls before removing a component.
Use it to clean up resources like timers or subscriptions.
Without cleanup, resources keep running causing memory leaks.
Implement ngOnDestroy() method and perform cleanup inside.
Angular calls it automatically when component is destroyed.
Full Transcript
In Angular, ngOnDestroy is a special method called when a component is about to be removed from the screen. This is the perfect time to stop timers, unsubscribe from data streams, or clean any resources to keep the app healthy. For example, if a component starts a timer, it keeps running even if the component is gone, unless we stop it in ngOnDestroy. The flow starts with component creation, then usage, then removal triggers ngOnDestroy, which runs cleanup code, and finally the component is removed. The execution table shows the timer starting, running, and stopping exactly when ngOnDestroy runs. Tracking the intervalId variable shows it changes from running to cleared. Beginners often wonder why cleanup is needed or when ngOnDestroy runs; it always runs just before removal. If cleanup is skipped, timers or subscriptions keep running, causing bugs or slowdowns. The quiz questions help check understanding of these steps and states.