0
0
Angularframework~20 mins

ngOnChanges for input changes in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular ngOnChanges Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when an @Input property changes in Angular?

Consider a child component with an @Input property. When the parent changes the input value, what does ngOnChanges receive?

Angular
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);
  }
}
AAn object with keys for each changed input, each containing previousValue and currentValue.
BA string with the new input value only.
CAn array of all input property names.
DNo argument is passed to ngOnChanges.
Attempts:
2 left
💡 Hint

Think about how Angular tracks changes to inputs and what information it provides.

state_output
intermediate
2:00remaining
What is logged when the input changes twice?

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'?

Angular
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}`);
  }
}
A
Changed from undefined to one
Changed from one to two
B
Changed from one to two
Changed from two to three
C
Changed from undefined to one
Changed from undefined to two
Changed from undefined to three
D
Changed from undefined to one
Changed from one to two
Changed from two to three
Attempts:
2 left
💡 Hint

Remember the first change has previousValue as undefined.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly implements ngOnChanges to detect input changes?

Choose the Angular component code that correctly implements ngOnChanges to detect changes on an input property count.

A
ngOnChanges(changes: SimpleChanges) {
  console.log(changes.currentValue);
}
B
ngOnChanges() {
  console.log('Count changed:', this.count);
}
C
ngOnChanges(changes: SimpleChanges) {
  if (changes.count) {
    console.log('Count changed:', changes.count.currentValue);
  }
}
D
ngOnChanges(changes: any) {
  console.log(changes.count.previousValue);
}
Attempts:
2 left
💡 Hint

Check how SimpleChanges is used to access changed inputs.

🔧 Debug
advanced
2:00remaining
Why does ngOnChanges not fire when an input object's property changes?

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';
ABecause the input property was not marked with @Input decorator.
BBecause ngOnChanges only works with primitive inputs, not objects.
CBecause the child component must implement ngDoCheck instead of ngOnChanges.
DBecause Angular compares input references, not deep object properties, so the reference did not change.
Attempts:
2 left
💡 Hint

Think about how Angular detects changes for objects passed as inputs.

🧠 Conceptual
expert
3:00remaining
What is the correct sequence of Angular lifecycle hooks when an input changes?

Order the lifecycle hooks in the sequence Angular calls them when an input property changes on a component.

A2, 1, 3, 4
B1, 2, 3, 4
C3, 2, 1, 4
D2, 3, 1, 4
Attempts:
2 left
💡 Hint

Remember which hooks run before and after initialization and which run on every change detection cycle.