I have some clarifications to make.
(1)Angular revolves around `components`. It employs a `hierarchy of components` as its core `architectural concept`.
(2)Modularity – significant core functionalities are now modular, resulting in a faster and lighter core.
Components primarily focus on a view of the application
and logic on the page
. A component consists of two crucial elements: a view
(.html) and some logic
(.ts).
In simple terms, components can be viewed as separate and autonomous views that can be utilized with other components (by passing component selector like <my-app></my-app>
).
For example, if you create a 'calendar component' with all the necessary logic implemented and it works effectively as an individual component, you can then use this component in other 'modules
' or even in any other 'app'
.
So why do we pass the selector in index.html?
Because:-
(1) When a new project is created in Angular using the command
ng new PROJECT-NAME
(refer https://github.com/angular/angular-cli), by default a single module named app.module.ts
(Parent Module) is created.
And, the selector of app.component.ts
is automatically passed in index.html
when a new project is created.
Take a look at:-
main.ts
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.log(err));
The answer to the question lies in main.ts
of the project which is generated by default upon project creation.
Now, in main.ts
:-
1) Parent Module is imported as `import { AppModule } from './app/app.module';` (Since default single module created
2) By default ('default' refers to when a new project is created), then...
`platformBrowserDynamic().bootstrapModule(AppModule)`.
This statement bootstraps the Module which is passed as a parameter inside it (by default, 'AppModule').
This means it will load the `module` which is specified as the bootstraped parameter.
Now, in app.module.ts
, the only default component created during the new project setup is app.component.ts
. Now, to view, the template for app.component.ts
i.e., app.component.html
, the selector is passed as
<app-root></app-root>
. (Passing selector reference for view, this, is an angular concept).
I hope that explanation helps clarify things.