Complete the code to implement the Angular lifecycle hook that runs after the component's view is initialized.
export class MyComponent implements [1] { ngAfterViewInit() { console.log('View is ready'); } }
The AfterViewInit interface is used to run code after the component's view has been fully initialized.
Complete the code to import the correct Angular interface for the ngAfterViewInit lifecycle hook.
import { Component, [1] } from '@angular/core'; @Component({ selector: 'app-sample', template: '<p>Sample works!</p>' }) export class SampleComponent implements [1] { ngAfterViewInit() { console.log('View initialized'); } }
You must import AfterViewInit to implement the ngAfterViewInit lifecycle hook.
Fix the error in the lifecycle hook method name to correctly detect when the view is ready.
export class DemoComponent implements AfterViewInit { [1]() { console.log('View is ready'); } }
The correct method name is ngAfterViewInit. Angular calls this method after the component's view is initialized.
Fill both blanks to correctly access a child element after the view is initialized.
import { Component, ViewChild, ElementRef, [1] } from '@angular/core'; @Component({ selector: 'app-child-access', template: '<p #paraRef>Text</p>' }) export class ChildAccessComponent implements [2] { @ViewChild('paraRef') para!: ElementRef; ngAfterViewInit() { console.log(this.para.nativeElement.textContent); } }
You must import and implement AfterViewInit to safely access view child elements after initialization.
Fill all three blanks to create a component that logs a message after the view is ready and accesses a button element.
import { Component, ViewChild, ElementRef, [1] } from '@angular/core'; @Component({ selector: 'app-button', template: '<button #btn>Click me</button>' }) export class ButtonComponent implements [2] { @ViewChild('btn') button!: ElementRef; [3]() { console.log('Button text:', this.button.nativeElement.textContent); } }
Import and implement AfterViewInit, then define the method ngAfterViewInit to run code after the view is initialized.