difference between a constructor and ngOnInit?

Constructor is a default method in TypeScript classes that are normally used for the initialization purpose. On the other hand, the ngOnInit is specifically an Angular method and is used to define Angular bindings. Even though constructors are getting called first, it is always preferred to move all of your Angular bindings to the ngOnInit method.

See the following example how we can use ngOnInit by implementing OnInit interface as follows:

  1. export class App implements OnInit{  
  2.   constructor(){  
  3.      //called first time before the ngOnInit()  
  4.   }  
  5.   ngOnInit(){  
  6.      //called after the constructor and called  after the first ngOnChanges()  
  7.   }  
  8. }