Bird
0
0

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

easy📝 Syntax Q3 of 15
Angular - Components
Which of the following is the correct way to implement the ngAfterViewInit lifecycle hook in an Angular component?
AAfterViewInit() { console.log('View initialized'); }
BngAfterViewInit() { console.log('View initialized'); }
CngAfterViewInit: function() { console.log('View initialized'); }
DngAfterViewInit => { console.log('View initialized'); }
Step-by-Step Solution
Solution:
  1. Step 1: Recall Angular lifecycle hook syntax

    Lifecycle hooks are methods named exactly, without arrow functions or colons.
  2. Step 2: Check each option

    ngAfterViewInit() { console.log('View initialized'); } uses correct method syntax. Options B, C, and D use incorrect syntax for Angular class methods.
  3. Final Answer:

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

    Correct hook method syntax = ngAfterViewInit() { console.log('View initialized'); } [OK]
Quick Trick: Use method syntax: ngAfterViewInit() { } [OK]
Common Mistakes:
  • Using arrow functions for hooks
  • Missing 'ng' prefix
  • Using object literal syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes