Hari
Hari

Reputation: 1623

When does Angular create component instances

I am new to angular, From the documentation i learned that we can launch the angular app by the bootstrap the root module, then angular can launch the app by creating an instance of app component and find the selector of this component in index.html and put the view of that root component, My question is whether angular use this same strategy for all components, for example does this create all the components same time or create components based directives declared on template.

1) i have 2 components such as app component, navbar component 2) app component have <app-navbar></app-navbar> on its template

From my understanding Angular first create root component and paste that component view in the root component selector(app-root) on index.html , if that root component view has another selector called app-navbar then only it create instance of navbar.component and paste that view in selector.correct me if i am wrong?

Upvotes: 3

Views: 1734

Answers (2)

Ranjeet Thorat
Ranjeet Thorat

Reputation: 170

Update

Angular will only instantiate a component

  1. It encounters a selector for that component
  2. If the component is provided in entryComponent array
  3. If component included in route definition

Upvotes: 0

Max Koretskyi
Max Koretskyi

Reputation: 105547

... and find the selector of this component in index.html and put the view of that root component

This only happens for components defined as bootstrap components in a module decorator. Hence here:

Angular first create root component

it's better to use term bootstrap component as there can be many bootstrap components.

i have 2 components such as app component, navbar component

Angular compiler will generate two factories. During that process it will encounter <app-navbar></app-navbar> and create appropriate view nodes. When Angular will be creating views it will create a view for navbar component and instantiate NavbarComponent class.

To know more about View start with the Here is why you will not find components inside Angular

Upvotes: 0

Related Questions