Bird
0
0

Which of the following is the correct way to implement ngAfterViewInit in an Angular component?

easy📝 Syntax Q12 of 15
Angular - Lifecycle Hooks
Which of the following is the correct way to implement ngAfterViewInit in an Angular component?
AngAfterViewInit => { console.log('View initialized'); }
BngAfterViewInit() { console.log('View initialized'); }
Cfunction ngAfterViewInit() { console.log('View initialized'); }
DngAfterViewInit: function() { console.log('View initialized'); }
Step-by-Step Solution
Solution:
  1. Step 1: Recall Angular component method syntax

    Angular lifecycle hooks are implemented as class methods without special syntax like arrow functions or function keywords.
  2. Step 2: Check each option's syntax

    ngAfterViewInit() { console.log('View initialized'); } uses the correct method syntax inside a class. Options A, B, and C use invalid or incorrect JavaScript syntax for class methods.
  3. Final Answer:

    ngAfterViewInit() { console.log('View initialized'); } -> Option B
  4. Quick Check:

    Use method() syntax for lifecycle hooks [OK]
Quick Trick: Use method() syntax inside class for hooks [OK]
Common Mistakes:
  • Using arrow functions for lifecycle hooks
  • Adding function keyword inside class
  • Using object literal syntax instead of class methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes