nakul bhatt
nakul bhatt

Reputation: 21

What is the exact meaning of component initialization in angular 2+, when we talk about OnInit?

According to Angular.io Angular Documentation on Life cycle hooks The purpose of OnInit : Initialize the directive/component after Angular first displays the data-bound properties and sets the directive/component's input properties.

So what exactly happens when it is said component has initialized. Does it mean all the variables are initialized or the component's template is displayed or both?

Upvotes: 1

Views: 3059

Answers (2)

Antoniossss
Antoniossss

Reputation: 32535

So what exactly happens when it is said component has initialized. Does it mean all the variables are initialized or the component's template is displayed or both?

Markup is rendered, bindings are bound, NO child view/components are injected. In other words, you can start initializing your component in context of its inputs. You can threat it as "Angular Construction" method and do all component initialization stuff there. It is very similar to @PostConstruct concept from EJB.

Upvotes: 1

Piyush Patel
Piyush Patel

Reputation: 1751

ngOnInit is useful after angular is done with creation of the component. So, when constructor finishes its work, the component is said to be initialized and it has specified properties which you can initialize in ngOnInit now.

I hope this helps!

Upvotes: 0

Related Questions