Bird
0
0

Given this Angular component code snippet:

medium📝 component behavior Q13 of 15
Angular - Lifecycle Hooks
Given this Angular component code snippet:
export class MyComponent implements OnInit, OnChanges {
  @Input() data: string;

  ngOnChanges() { console.log('Changes detected'); }
  ngOnInit() { console.log('Init called'); }
}

What will be logged first when the component receives its first input value?
AInit called
BChanges detected
CNo logs
DBoth logs at the same time
Step-by-Step Solution
Solution:
  1. Step 1: Understand lifecycle order on first input

    When the component first receives input, Angular calls ngOnChanges before ngOnInit.
  2. Step 2: Identify which log appears first

    The ngOnChanges method logs 'Changes detected' first, then ngOnInit logs 'Init called'.
  3. Final Answer:

    Changes detected -> Option B
  4. Quick Check:

    ngOnChanges runs before ngOnInit [OK]
Quick Trick: ngOnChanges logs before ngOnInit on first input [OK]
Common Mistakes:
  • Assuming ngOnInit runs before ngOnChanges
  • Thinking logs happen simultaneously
  • Ignoring ngOnChanges on first input

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes