Lifecycle Hooks and Change Detection Basics
The Life of a Component
text
constructor -> ngOnChanges -> ngOnInit -> ngDoCheck
-> ngAfterContentInit -> ngAfterViewInit
... (updates repeat) ...
-> ngOnDestroy
| Hook | When | Use it for |
|---|---|---|
constructor | Class created | Only dependency injection (inject()) and simple setup |
ngOnChanges | An @Input changed | React to old-style input changes |
ngOnInit | After first inputs are set | Load data, start work |
ngAfterViewInit | View and children ready | Work that needs the DOM, charts |
ngOnDestroy | Just before removal | Clean up: unsubscribe, timers, listeners |
ngOnInit and ngOnDestroy
typescript
import { Component, OnInit, OnDestroy, inject } from '@angular/core';
@Component({ selector: 'app-clock', template: `<p>{{ time }}</p>` })
export class Clock implements OnInit, OnDestroy {
time = '';
private timerId?: number;
ngOnInit() {
this.timerId = window.setInterval(() => {
this.time = new Date().toLocaleTimeString();
}, 1000);
}
ngOnDestroy() {
clearInterval(this.timerId); // always clean up
}
}
Cleanup with DestroyRef (modern)
typescript
export class Clock {
constructor() {
const id = setInterval(() => console.log('tick'), 1000);
inject(DestroyRef).onDestroy(() => clearInterval(id));
}
}
RxJS Subscriptions: takeUntilDestroyed
typescript
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
constructor() {
interval(1000).pipe(takeUntilDestroyed()).subscribe(n => console.log(n));
}
Memory leaks: A subscription that is never ended keeps running after the component is gone. Use
takeUntilDestroyed, the async pipe or toSignal.
Run Code After Rendering
Use afterNextRender and afterRenderEffect for code that needs the real DOM, such as measuring an element or starting a chart library. They do not run on the server.
typescript
constructor() {
afterNextRender(() => {
this.chart = new Chart(this.canvas().nativeElement, config);
});
}
Change Detection
Change detection is how Angular finds out that data changed so it can update the screen.
| Strategy | Angular checks the component when |
|---|---|
| Default | Anything might have changed (after any event, timer or HTTP call). Easy but does more work |
OnPush | An input reference changes, an event happens in it, a signal it reads changes or markForCheck() is called |
typescript
@Component({
selector: 'app-user-row',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `<p>{{ user().name }}</p>`,
})
export class UserRow { user = input.required<User>(); }
Signals fit OnPush perfectly: When a template reads a signal, Angular knows exactly which component to refresh. This is why modern Angular is moving to signal-first and zoneless change detection (see the performance lessons).
Rule of thumb: Use
OnPush and signals in new components, keep ngOnInit for loading data and always clean up what you start.
Conclusion
Use ngOnInit to start work, ngOnDestroy or DestroyRef to clean up and afterNextRender for DOM work. Combine OnPush with signals for efficient updates.
Quick Summary: ngOnInit start, ngOnDestroy stop; OnPush + signals.

