You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The statement it is preferred to move all of your Angular bindings to ngOnInit method is wrong, constructor has its own job as well as ngOnInit.
I think this answer is more applicable:
Constructor is the default method for a class that is created when a class is installed and ensures the proper execution of the roles in the class and its subsections. Angular is preferably the Dependency Injector (DI), analyzes the builder’s components and when creating a new feature by calling the new MyClass() tries to find suppliers that match the builder’s parameter types, resolve them and pass them to similar components.
ngOnInit: OnInit is a life cycle widget called Angular to show that Angular is made to create a component.
Differences:
We use constructor() for all the initialization/declaration.
It’s better to avoid writing actual work in the constructor.
The constructor() should only be used to initialize class members but shouldn’t do actual “work”.
So we should use constructor() to set up Dependency Injection, Initialization of class fields, etc.
ngOnInit() is a better place to write “actual work code” that we need to execute as soon as the class is instantiated.
Like loading data from Database — to show the user in your HTML template view. Such code should be written in ngOnInit().
The text was updated successfully, but these errors were encountered:
it is preferred to move all of your Angular bindings to ngOnInit method
is wrong,constructor
has its own job as well asngOnInit
.I think this answer is more applicable:
Constructor is the default method for a class that is created when a class is installed and ensures the proper execution of the roles in the class and its subsections. Angular is preferably the Dependency Injector (DI), analyzes the builder’s components and when creating a new feature by calling the new MyClass() tries to find suppliers that match the builder’s parameter types, resolve them and pass them to similar components.
ngOnInit: OnInit is a life cycle widget called Angular to show that Angular is made to create a component.
Differences:
We use constructor() for all the initialization/declaration.
It’s better to avoid writing actual work in the constructor.
The constructor() should only be used to initialize class members but shouldn’t do actual “work”.
So we should use constructor() to set up Dependency Injection, Initialization of class fields, etc.
ngOnInit() is a better place to write “actual work code” that we need to execute as soon as the class is instantiated.
Like loading data from Database — to show the user in your HTML template view. Such code should be written in ngOnInit().
The text was updated successfully, but these errors were encountered: