Difference between constructor and ngOnInit in Angular2

I've been looking at angular 2 documentation on lifecycles but it's not clear to me.

What is the difference between the constructor and ngOnInit?

 5
Author: devconcept, 2016-10-13

1 answers

The constructor is used for lighter tasks.

NgOnInit is a special hook intended for more "heavy" tasks, like bringing the provider data or things like that. Angular recommends leaving the constructors for very light tasks, and the rest leave it for ngOnInit.

NgOnInit will be invoked only once (assumed after the first onChanges, but in practice it is called at element creation, as if the constructor was invoked at Class creation).

Here https://angular.io/docs/ts/latest/tutorial/toh-pt4.html explain it a little more in detail (in " the ngOnInit lifecycle Hook")

Regarding the rest of hooks, onchanges and ondestroy don't have much science and are more or less self-explanatory. The rest is detailed in https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html

 7
Author: Mik Pride, 2016-10-13 10:21:05