Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Component Constructor Vs OnInit [duplicate]

If I want function x to happen every time a component loads, whether its the first time, I navigate to a different site and navigate back or it's the fifth time the component has loaded.

What should I put function x in? The component constructor or OnInit?

like image 914
ClickThisNick Avatar asked Mar 07 '16 13:03

ClickThisNick


People also ask

What is the exact difference between constructors and OnInit in Angular?

The main difference between constructor and ngOnInit is that ngOnInit is lifecycle hook and runs after constructor. Component interpolated template and input initial values aren't available in constructor, but they are available in ngOnInit . The practical difference is how ngOnInit affects how the code is structured.

What is the difference between ngOnInit () and constructor () of a component?

The constructor() should only be used to initialize class members but shouldn't do actual "work". So we should use constructor() to setup 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.

What is the difference between constructor and ngOnInit in Angular 2?

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.

Is constructor must in Angular?

In the Angular constructor, a constructor argument with dependency type injects the dependencies. It is a must to keep the Angular component constructor simple throughout.


2 Answers

Constructor is predefined default method of the typescript class. There is no relation between Angular and constructor. Normally we use constructor to define/initialize some variables, but when we have tasks related to Angular's bindings we move to Angular's ngOnInit life cycle hook. ngOnInit is called just after the constructor call. We can also do the same work in the constructor but its preferable to use ngOnInit to start Angular's binding.

in order to use ngOnInit we have to import this hook from the core library:

import {Component, OnInit} from '@angular/core' 

Then we implement this interface with exported class (this is not compulsory to implement this interface but generally we did).

Example of using both:

export class App implements OnInit{   constructor(){      //called first time before the ngOnInit()   }    ngOnInit(){      //called after the constructor and called  after the first ngOnChanges()    } } 

For more detail see also Difference between Constructor and ngOnInit

like image 187
Pardeep Jain Avatar answered Sep 17 '22 13:09

Pardeep Jain


The first one (constructor) is related to the class instantiation and has nothing to do with Angular2. I mean a constructor can be used on any class. You can put in it some initialization processing for the newly created instance.

The second one corresponds to a lifecycle hook of Angular2 components:

  • ngOnChanges is called when an input or output binding value changes
  • ngOnInit is called after the first ngOnChanges

So you should use ngOnInit if initialization processing of your function relies on bindings of the component (for example component parameters defined with @Input), otherwise the constructor would be enough...

like image 30
Thierry Templier Avatar answered Sep 16 '22 13:09

Thierry Templier