The problem I came across was this: I needed to validate one
token when the page was started if the token was then validated
would be visible a second Forms if not only the first Forms where
would I call that function? in ngOnInit or constructor?
In the ngOnInit
, because it can be used precisely for this type of thing, avoid logic in the constructor.
If the angular already provides ngOnInit pq there is also a constructor?
Two things for different purposes:
The constructor
is the default method of the class. In Angular it is mainly used to inject dependencies into the component
The ngOnInit
is part of the component’s life cycle. A common practice is to use it for initialization of component logic.
When to use ngOnInit or constructor?
Usually the two are used together, because each has a different purpose, as stated earlier.
Interesting article that talks a little about