Lifecycle Hooks
Lifecycle Hooks
Section titled βLifecycle HooksβWhat it means
Section titled βWhat it meansβAngular calls specific methods on a component at defined points in its life β creation, updates, and destruction β if you implement them. The most common: ngOnInit (after Angular first sets the componentβs inputs β the standard place for initial setup, not the constructor), ngOnChanges (whenever an @Input() changes), and ngOnDestroy (right before the component is removed β the place to clean up subscriptions, timers, etc.).
Examples
Section titled βExamplesβimport { Component, Input, OnInit, OnChanges, OnDestroy, SimpleChanges } from '@angular/core';
@Component({ selector: 'app-widget', template: `...` })export class WidgetComponent implements OnInit, OnChanges, OnDestroy { @Input() userId!: string; private subscription?: Subscription;
ngOnInit() { // runs once, after inputs are first set -- do initial data loading here this.subscription = this.userService.getUser(this.userId).subscribe(/* ... */); }
ngOnChanges(changes: SimpleChanges) { // runs whenever an @Input() value changes, including the very first time if (changes['userId']) { console.log('userId changed to', changes['userId'].currentValue); } }
ngOnDestroy() { // runs right before the component is removed -- always clean up here this.subscription?.unsubscribe(); }}Common mistake
Section titled βCommon mistakeβPutting initialization logic that depends on @Input() values in the constructor instead of ngOnInit() β Angular hasnβt set input values yet when the constructor runs, so theyβll be undefined at that point.
export class WidgetComponent { @Input() userId!: string;
constructor(private userService: UserService) { console.log(this.userId); // undefined -- inputs aren't set yet! this.userService.getUser(this.userId); // fails, called with undefined }}
// Fix: move input-dependent logic to ngOnInitexport class WidgetComponent implements OnInit { @Input() userId!: string; constructor(private userService: UserService) {} // constructor: only DI, no input-dependent logic
ngOnInit() { console.log(this.userId); // set correctly by now }}Quick practice
Section titled βQuick practiceβ-
Why shouldnβt you rely on
@Input()values inside a componentβs constructor?Answer
Angular sets input values after the constructor runs, so they're stillundefinedat that point β input-dependent logic belongs inngOnInit()instead. -
What is
ngOnDestroytypically used for?Answer
Cleanup β unsubscribing from Observables, clearing timers/intervals, and removing event listeners before the component is removed, to prevent memory leaks. -
Does
ngOnChangesrun on the very first input assignment, or only on subsequent changes?Answer
It also runs on the first assignment (right beforengOnInit), not just on later changes.