Angular - Lifecycle HooksYou 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 initializedBngOnInit, because it runs after input properties are setCngAfterContentInit, because it runs after projected content is initializedDngDoCheck, because it runs during every change detectionCheck Answer
Step-by-Step SolutionSolution:Step 1: Identify hook for view initializationngAfterViewInit runs after the component's view and child views are fully initialized.Step 2: Compare with other hooksngOnInit runs earlier; ngAfterContentInit is for projected content; ngDoCheck runs repeatedly during change detection.Final Answer:ngAfterViewInit, because it runs after the component's view and child views are initialized -> Option AQuick Check:View init hook = ngAfterViewInit [OK]Quick Trick: Use ngAfterViewInit for view and child view setup [OK]Common Mistakes:Confusing content init with view initUsing ngOnInit for view-dependent codeMisusing ngDoCheck for initialization
Master "Lifecycle Hooks" in Angular9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More Angular Quizzes Angular Fundamentals - Angular CLI installation and setup - Quiz 12easy Component Interaction - @ContentChild and content projection - Quiz 13medium Component Interaction - @Input decorator for parent to child - Quiz 6medium Components - Creating components with CLI - Quiz 7medium Directives - *ngFor for list rendering - Quiz 9hard Lifecycle Hooks - ngOnInit for initialization - Quiz 11easy Templates and Data Binding - Why data binding matters - Quiz 15hard Templates and Data Binding - Two-way binding with ngModel - Quiz 15hard TypeScript in Angular - Type annotations in components - Quiz 3easy TypeScript in Angular - Interfaces for data models - Quiz 2easy