Consider a child component with an @Input property. When the parent changes the input value, what does ngOnChanges receive?
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core'; @Component({ selector: 'app-child', template: `<p>{{data}}</p>` }) export class ChildComponent implements OnChanges { @Input() data!: string; ngOnChanges(changes: SimpleChanges) { console.log(changes); } }
Think about how Angular tracks changes to inputs and what information it provides.
The ngOnChanges method receives a SimpleChanges object. This object has keys for each input property that changed. Each key holds an object with previousValue, currentValue, and firstChange properties.
Given this Angular component, what will be logged to the console after the parent changes the input from 'one' to 'two' and then to 'three'?
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core'; @Component({ selector: 'app-child', template: `<p>{{value}}</p>` }) export class ChildComponent implements OnChanges { @Input() value!: string; ngOnChanges(changes: SimpleChanges) { console.log(`Changed from ${changes['value'].previousValue} to ${changes['value'].currentValue}`); } }
Remember the first change has previousValue as undefined.
On the first input change (initial binding), previousValue is undefined. Subsequent changes show the last value as previousValue. The prompt describes subsequent changes starting from 'one', so the logs are 'Changed from one to two' followed by 'Changed from two to three'.
Choose the Angular component code that correctly implements ngOnChanges to detect changes on an input property count.
Check how SimpleChanges is used to access changed inputs.
Option C correctly checks if the count property changed and accesses its currentValue. Option C lacks the changes parameter and cannot detect previous values. Option C uses any but does not check if count exists. Option C tries to access changes.currentValue which is invalid.
Given this parent and child component, why does ngOnChanges in the child not trigger when a property inside the input object changes?
Parent template:
<app-child [config]="config"></app-child>
Parent component:
config = { theme: 'dark' };
// Later update
this.config.theme = 'light';
Think about how Angular detects changes for objects passed as inputs.
Angular's change detection for inputs uses reference checks. If the object reference does not change, ngOnChanges does not fire, even if internal properties change. To detect such changes, you must replace the object with a new reference or use other lifecycle hooks.
Order the lifecycle hooks in the sequence Angular calls them when an input property changes on a component.
Remember which hooks run before and after initialization and which run on every change detection cycle.
When an input changes, Angular calls ngOnChanges first, then ngOnInit (only once after first change), then ngDoCheck on every change detection, and finally ngAfterViewInit after the component's view is initialized.