-7

It seems to be similar functions, what are the main differences and purposes of them

//Code

import { OnInit } from '@angular/core';
constructor(){


}

ngOnInit(){

}
Antoine Delia
  • 1,728
  • 5
  • 26
  • 42
  • 2
    See: https://angular.io/guide/lifecycle-hooks – Harun Yilmaz Apr 09 '19 at 11:34
  • 3
    contructor is a class constructor, `ngOnInit` is an Angular hook. The main difference is `@Input`s will be resolved on `ngOnInit`, but not in contructor. Also search the web before posting questions. https://stackoverflow.com/questions/35763730/difference-between-constructor-and-ngoninit – Roberto Zvjerković Apr 09 '19 at 11:35
  • 12
    Possible duplicate of [Difference between Constructor and ngOnInit](https://stackoverflow.com/questions/35763730/difference-between-constructor-and-ngoninit) – Roberto Zvjerković Apr 09 '19 at 11:35

1 Answers1

0

People have already mentioned life cycle hooks so I won't talk about that. However, Angular docs state that in the constructor you should only do lightweight operations such as simple sync variable assignments.

In ngOnInit you should do more heavy lifting operations such as HTTP requests, etc

danday74
  • 52,471
  • 49
  • 232
  • 283