NG.I · Angular 22 practice question

A developer implements `ngOnChanges` in a component that receives an input property. If this component is initialized and the input property is set immediately upon creation, when will the developer's logic inside `ngOnChanges` execute relative to the component's setup lifecycle?

import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';

@Component({ selector: 'app-profile', standalone: true }) 
export class ProfileComponent implements OnChanges {
  @Input({ required: true }) userId!: number;

  ngOnChanges(changes: SimpleChanges): void {
    // Logic here
  }
}
Choose one answer
Check answer and explanation

Correct answer: It runs before the component's template is rendered, and is executed even for the initial input value.

Explanation

The appropriate mechanism is the one that executes prior to the component's template rendering and is triggered even when the input value is set for the first time. This aligns with documentation indicating that `ngOnChanges` fires before the template check, and that the initial invocation of `ngOnChanges` happens before `ngOnInit` completes its setup.

Common trap

The assertion that the execution occurs after `ngOnInit` finishes is inaccurate because the documentation specifies that `ngOnChanges` precedes the template check, which happens before `ngOnInit` is fully executed. Furthermore, the claim that it only triggers upon a transition between distinct input states is false, as the documentation confirms the initial execution occurs during setup.

Source: Angular docs: Component Lifecycle — ngOnChanges
Checked against the cited text on 2026-11-25.

Report an error in this question