javascript – Difference between constructor and ngOnInit in Angular2


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

What is the difference between constructor and ngOnInit?


The constructor is used for lighter tasks.

ngOnInit is a special hook for more "heavy" tasks, like fetching provider data or things like that. Angular recommends leaving the constructors for very light tasks, and leaving the rest 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 they explain it a little more in detail (in " The ngOnInit Lifecycle Hook ")

Regarding the rest of hooks, onchanges and ondestroy they do not have much science and are more or less self-explanatory. The rest is detailed at

Scroll to Top