This example loops through all changed inputs and logs their old and new values.
Angular
ngOnChanges(changes: SimpleChanges) {
for (const propName in changes) {
const change = changes[propName];
console.log(`${propName} changed from ${change.previousValue} to ${change.currentValue}`);
}
}
Sample Program
This example has a parent component with a button that changes a string. The child component receives this string as input and uses ngOnChanges to log when the input changes.
A. ngOnChanges must have a parameter of type SimpleChanges
B. The @Input decorator is missing parentheses
C. ngOnChanges should return a value
D. data property must be initialized
Solution
Step 1: Check ngOnChanges method signature
The ngOnChanges method must accept a parameter of type SimpleChanges to receive input changes.
Step 2: Verify other parts
The @Input decorator syntax is correct, no return value is needed, and data can be uninitialized initially.
Final Answer:
ngOnChanges must have a parameter of type SimpleChanges -> Option A
Quick Check:
ngOnChanges(SimpleChanges) required [OK]
Hint: Always include SimpleChanges parameter in ngOnChanges [OK]
Common Mistakes:
Omitting the SimpleChanges parameter
Expecting ngOnChanges to return a value
Misusing @Input decorator syntax
5. You want to reset a component's internal counter to zero whenever the input resetFlag changes from false to true. Which ngOnChanges implementation correctly achieves this?
export class CounterComponent implements OnChanges {
@Input() resetFlag = false;
@Input() otherValue: number = 0;
counter = 10;
ngOnChanges(changes: SimpleChanges) {
// Which option resets counter correctly?
}
}
hard
A. if (this.resetFlag) { this.counter = 0; }
B. if (changes['resetFlag'] && changes['resetFlag'].previousValue === true) { this.counter = 0; }
C. if (changes['resetFlag'].previousValue === false) { this.counter = 0; }
D. if (changes['resetFlag'] && changes['resetFlag'].currentValue === true) { this.counter = 0; }
Solution
Step 1: Detect resetFlag input change to true
We must check if resetFlag changed and its new value is true.
Step 2: Verify condition logic
if (changes['resetFlag'] && changes['resetFlag'].currentValue === true) { this.counter = 0; } checks both existence of change and currentValue === true, correctly resetting counter.
Step 3: Analyze other options
if (this.resetFlag) { this.counter = 0; } checks current value but runs on any input change, incorrectly resetting if resetFlag is true when other inputs change. if (changes['resetFlag'].previousValue === false) { this.counter = 0; } accesses changes['resetFlag'] without checking existence, causing runtime error when other inputs change. if (changes['resetFlag'] && changes['resetFlag'].previousValue === true) { this.counter = 0; } checks previous true, wrong condition.
Final Answer:
if (changes['resetFlag'] && changes['resetFlag'].currentValue === true) { this.counter = 0; } -> Option D
Quick Check:
Check currentValue true on input change [OK]
Hint: Check currentValue === true in changes to reset [OK]
Common Mistakes:
Ignoring previousValue and currentValue difference
Resetting counter on wrong condition
Not checking if changes object has resetFlag
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.