Vikas
Difference Between Constructor And ngOnInit In Angular ?
By Vikas in Angular on Dec 20 2019
  • Wasim Hajwane
    Oct, 2020 1

    constructor -
    Compnent has a typescript class,So every component must have constructor.The constructor of a component class execute first before any execution of lifecycle hook event.
    If you want to inject any dependicies into a component then constructor is best place to inject those dependcies.

    ngOnInit-it can be called after constructor execution and before ngDocheck() methods.
    This event is mainly used for the initialize data in a component.

    • 1
  • Kiran Mohanty
    Sep, 2020 2

    Constructor fires as soon as your application runs, as we register all components under module. But ngOnInit fires only when application loads that particular component.

    If you have to do any API call, it’s advisable to define inside ngOnInit. We can do component property initialization inside constructor.

    • 1


Most Popular Job Functions


MOST LIKED QUESTIONS