Angular - ComponentsWhich 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'); }Check Answer
Step-by-Step SolutionSolution:Step 1: Recall Angular lifecycle hook syntaxLifecycle hooks are methods named exactly, without arrow functions or colons.Step 2: Check each optionngAfterViewInit() { console.log('View initialized'); } uses correct method syntax. Options B, C, and D use incorrect syntax for Angular class methods.Final Answer:ngAfterViewInit() { console.log('View initialized'); } -> Option BQuick Check:Correct hook method syntax = ngAfterViewInit() { console.log('View initialized'); } [OK]Quick Trick: Use method syntax: ngAfterViewInit() { } [OK]Common Mistakes:Using arrow functions for hooksMissing 'ng' prefixUsing object literal syntax
Master "Components" in Angular9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More Angular Quizzes Angular Modules - Shared modules for reusable components - Quiz 8hard Angular Modules - Module lazy loading preview - Quiz 10hard Angular Modules - Module lazy loading preview - Quiz 6medium Component Interaction - @ContentChild and content projection - Quiz 3easy Component Interaction - Template reference for direct access - Quiz 3easy Directives - *ngSwitch for multiple conditions - Quiz 13medium Directives - ngClass for dynamic classes - Quiz 6medium Lifecycle Hooks - ngOnDestroy for cleanup - Quiz 5medium Pipes - Creating custom pipes - Quiz 7medium Templates and Data Binding - Template reference variables - Quiz 7medium