In Angular projects we can often see classes but we do not see instances or keyword new
. My question is, at what time does Angular instances classes? At what point does the constructor
or ngOnInit()
method run?
Asked
Active
Viewed 37 times
-1
-
Latter question: [difference between Constructor and OnInit](https://stackoverflow.com/questions/35763730/difference-between-constructor-and-ngoninit) – AT82 Jul 15 '17 at 11:33
1 Answers
0
They're instantiated (and thus the constructor is run) when they need to be.
For example, a component is instantiated when the component needs to be rendered, either by being routed to, or invoked via <some-component>
in some template. A service is instantiated when it needs to be provided.
ngOnInit
is a life-cycle hook particular to components. You can read more about it vs. constructors in this question.