0
0
Angularframework~10 mins

ngOnChanges for input changes in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ngOnChanges for input changes
Parent changes input value
Angular detects input change
Calls ngOnChanges() in child
ngOnChanges receives SimpleChanges object
Child updates based on changes
View updates with new input
When a parent changes an input property, Angular calls ngOnChanges in the child with details of the change, allowing the child to react before the view updates.
Execution Sample
Angular
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';

@Component({ selector: 'child-comp', template: '{{data}}' })
export class ChildComponent implements OnChanges {
  @Input() data = '';
  ngOnChanges(changes: SimpleChanges) {
    console.log('Data changed:', changes['data'].currentValue);
  }
}
This child component logs changes to its input property 'data' whenever the parent updates it.
Execution Table
StepParent changes inputngOnChanges calledSimpleChanges contentChild actionView update
1data = 'Hello'Yes{ data: { previousValue: undefined, currentValue: 'Hello', firstChange: true } }Logs: Data changed: HelloView shows 'Hello'
2data = 'World'Yes{ data: { previousValue: 'Hello', currentValue: 'World', firstChange: false } }Logs: Data changed: WorldView shows 'World'
3data = 'World'NoNo change detectedNo logView remains 'World'
4data = 'Angular'Yes{ data: { previousValue: 'World', currentValue: 'Angular', firstChange: false } }Logs: Data changed: AngularView shows 'Angular'
💡 No further input changes, ngOnChanges not called.
Variable Tracker
VariableStartAfter 1After 2After 3After 4
data'' (empty)'Hello''World''World''Angular'
Key Moments - 3 Insights
Why is ngOnChanges not called when the input value stays the same?
Because Angular only calls ngOnChanges when it detects a change in the input value. Step 3 in the execution_table shows no call since 'data' remains 'World'.
What does the SimpleChanges object contain?
It contains previousValue, currentValue, and firstChange for each changed input. See steps 1, 2, and 4 in the execution_table for examples.
Can ngOnChanges detect multiple input changes at once?
Yes, ngOnChanges receives all changed inputs in the SimpleChanges object. This example shows one input, but Angular supports multiple inputs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'data' after step 2?
A'Hello'
B'World'
C'Angular'
D'' (empty string)
💡 Hint
Check the 'data' variable in variable_tracker after 'After 2'
At which step does ngOnChanges NOT get called?
AStep 1
BStep 4
CStep 3
DStep 2
💡 Hint
Look at the 'ngOnChanges called' column in execution_table
If the parent changes 'data' to the same value twice in a row, what happens?
AngOnChanges is not called the second time
BngOnChanges is called once
CngOnChanges is called twice
DThe view does not update at all
💡 Hint
Refer to step 3 in execution_table where input stays the same
Concept Snapshot
ngOnChanges is a lifecycle hook called when Angular detects input property changes.
It receives a SimpleChanges object with previous and current values.
Use it to react to input changes before the view updates.
Called only when input values actually change.
Useful for initialization or side effects on input update.
Full Transcript
This visual execution shows how Angular calls ngOnChanges in a child component when its input property changes. When the parent updates the input, Angular detects the change and calls ngOnChanges with a SimpleChanges object describing the old and new values. The child can then react, such as logging the new value or updating internal state. If the input value does not change, ngOnChanges is not called. This helps components respond only to real changes. The view updates after ngOnChanges runs, showing the latest input value.