Bird
0
0

You want to run code after Angular initializes the component's view and its child views. Which lifecycle hook should you use and why?

hard📝 Conceptual Q8 of 15
Angular - Lifecycle Hooks
You want to run code after Angular initializes the component's view and its child views. Which lifecycle hook should you use and why?
AngAfterViewInit, because it runs after the component's view and child views are initialized
BngOnInit, because it runs after input properties are set
CngAfterContentInit, because it runs after projected content is initialized
DngDoCheck, because it runs during every change detection
Step-by-Step Solution
Solution:
  1. Step 1: Identify hook for view initialization

    ngAfterViewInit runs after the component's view and child views are fully initialized.
  2. Step 2: Compare with other hooks

    ngOnInit runs earlier; ngAfterContentInit is for projected content; ngDoCheck runs repeatedly during change detection.
  3. Final Answer:

    ngAfterViewInit, because it runs after the component's view and child views are initialized -> Option A
  4. Quick Check:

    View init hook = ngAfterViewInit [OK]
Quick Trick: Use ngAfterViewInit for view and child view setup [OK]
Common Mistakes:
  • Confusing content init with view init
  • Using ngOnInit for view-dependent code
  • Misusing ngDoCheck for initialization

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes