As constructor is a default function called by class, and NgOnInit is a default lifecycle hook called by angular component, so why we don't initialize class members in NgOnInit()?
Asked
Active
Viewed 35 times
0
-
1Does this answer your question? [Difference between Constructor and ngOnInit](https://stackoverflow.com/questions/35763730/difference-between-constructor-and-ngoninit) – Alan Yu Nov 22 '21 at 08:10
-
When you use as arguments some directive in constructor you're injecting (not defining), see the [docs](https://angular.io/guide/dependency-injection#injecting-services) – Eliseo Nov 22 '21 at 09:45
-
@AlexYu No its just difference – Abhishek Upadhyay Nov 23 '21 at 02:36
-
@Eliseo I know but i am asking a bit different Question – Abhishek Upadhyay Nov 23 '21 at 02:38
-
1@AbhishekUpadhyay the first answer already said: **The Constructor is a default method of the class that is executed when the class is instantiated and ensures proper initialisation of fields in the class and its subclasses.** – Alan Yu Nov 23 '21 at 03:25