Lifecycle Hooks and Change Detection Basics

Angular 14 min min read Updated: Nov 16, 2026 Intermediate
Lifecycle Hooks and Change Detection Basics
Intermediate Topic 6 of 7

The Life of a Component

text constructor -> ngOnChanges -> ngOnInit -> ngDoCheck -> ngAfterContentInit -> ngAfterViewInit ... (updates repeat) ... -> ngOnDestroy
HookWhenUse it for
constructorClass createdOnly dependency injection (inject()) and simple setup
ngOnChangesAn @Input changedReact to old-style input changes
ngOnInitAfter first inputs are setLoad data, start work
ngAfterViewInitView and children readyWork that needs the DOM, charts
ngOnDestroyJust before removalClean 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.

StrategyAngular checks the component when
DefaultAnything might have changed (after any event, timer or HTTP call). Easy but does more work
OnPushAn 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.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators