TypeScript classes has a default method called constructor which is normally used for the initialization purpose. Whereas ngOnInit method is specific to Angular, especially used to define Angular bindings. Even though constructor getting called first, it is preferred to move all of your Angular bindings to ngOnInit method. In order to use ngOnInit, you need to implement OnInit interface as below,
export class App implements OnInit{
constructor(){
//called first time before the ngOnInit()
}
ngOnInit(){
//called after the constructor and called after the first ngOnChanges()
}
}
Posted Date:- 2021-09-09 04:38:50
What Is Package.json? Explain its Purpose
What Is the Difference Between Class Decorators and Class Field Decorators?
What type of DOM does Angular implement?
What is Eager and Lazy loading?
What is Bootstrap? How is it embedded into Angular?
How Can You Disable All the Animations in Angular?
Is the Routing Module Mandatory for an Application?
Explain the Importance of HttpClient.
What Is the Purpose of an Async Pipe?
Which Is the Latest Version of Angular? What Are the New Features in It?
What are Promises and Observables in Angular?
What is the difference between AOT and JIT?
Explain the lifecycle hooks in Angular
What do you understand by scope in Angular?
What Is the Primary Language Used in Angular?
What is the difference between constructor and ngOnInit?
What are the differences between Angular decorator and annotation?
What do you mean by string interpolation?
What is view encapsulation in Angular?
What are filters in Angular? Name a few of them.
What is the PipeTransform interface?
What is the purpose of ngFor directive?
What is the option to choose between inline and external template file?
What is an AOT compilation? What are its advantages?
What are Annotations in Angular?
What are decorators in Angular?
What are Single Page Applications (SPA)?
What do you mean by data binding?
Could you explain services in Angular?
Describe the MVVM architecture.
Could we make an angular application to render on the server-side?
Define the ng-content Directive?
Why was Angular introduced as a client-side framework?
What are some advantages of using Angular?
What are the key components of Angular?
Why were client-side frameworks like Angular introduced?