Complete the code to implement the Angular lifecycle hook for initialization.
export class AppComponent implements OnInit { constructor() { // Initialization code here } [1]() { console.log('Component initialized'); } }
The ngOnInit method is the Angular lifecycle hook called once after the component is initialized. It is used to run initialization logic.
Complete the code to import the correct Angular interface for initialization.
import { Component, [1] } from '@angular/core'; @Component({ selector: 'app-root', template: '<h1>Hello</h1>' }) export class AppComponent implements OnInit { ngOnInit() { console.log('Initialized'); } }
The OnInit interface must be imported from @angular/core to use the ngOnInit lifecycle hook.
Fix the error in the lifecycle hook method name to properly initialize the component.
export class SampleComponent implements OnInit { [1]() { console.log('Component ready'); } }
The lifecycle hook method must be named exactly ngOnInit with correct casing to be called by Angular.
Fill both blanks to correctly implement the initialization lifecycle hook in an Angular component.
import { Component, [1] } from '@angular/core'; @Component({ selector: 'app-demo', template: '<p>Demo works!</p>' }) export class DemoComponent implements [2] { ngOnInit() { console.log('Demo initialized'); } }
You must import OnInit and implement it in the component class to use ngOnInit properly.
Fill all three blanks to create a component that logs a message on initialization using ngOnInit.
import { Component, [1] } from '@angular/core'; @Component({ selector: 'app-log', template: '<div>Logging Component</div>' }) export class LogComponent implements [2] { constructor() { console.log('Constructor called'); } [3]() { console.log('ngOnInit called'); } }
To use ngOnInit, import and implement OnInit and define the ngOnInit method in the component.