Sub topic 6
The root component and standalone components
This chapter assumes you know basic TypeScript (classes and decorators), basic HTML and CSS, and that you have run ng new and ng serve once. It covers the root component that main.ts bootstraps, and the standalone model that Angular uses to declare what a component depends on.
What the root component is
The root component is the top of the component tree. bootstrapApplication takes it as its first argument, creates it, and attaches it to the host element in src/index.html. Every other component renders inside it, either directly in its template or through the router.
The CLI generates three files for it in src/app/:
app.component.ts: the class and its@Componentmetadata.app.component.html: the template.app.component.css: styles scoped to this component. The extension depends on the stylesheet format you chose inng new.
Newer CLI versions name these app.ts, app.html and app.css, and name the class App. The structure is the same. This chapter uses the older AppComponent names, so substitute the names your CLI generated.
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
imports: [RouterOutlet],
templateUrl: './app.component.html',
styleUrl: './app.component.css',
})
export class AppComponent {
title = 'my-app';
}How the pieces connect to index.html
The selector value app-root is a CSS selector. index.html contains an <app-root></app-root> element. When bootstrapApplication(AppComponent, appConfig) runs, Angular finds the element that matches the selector and renders the component's template into it.
<body>
<app-root></app-root>
</body>If you rename the selector in the component but not in index.html, the page stays blank. The browser console reports an error that the root element for the component was not found. Change both together.
The app- prefix comes from the prefix property in angular.json. The CLI applies it to selectors of generated components, which avoids clashes with standard HTML elements and third-party components.
Use the diagram below to follow the startup path.
What "standalone" means
A standalone component declares its own dependencies. Before standalone components, every component belonged to an NgModule, and the module's declarations and imports arrays decided which other components, directives and pipes the template could use. Standalone components move that information into the component itself, through the imports array in @Component.
Standalone is the default from Angular 19. In Angular 17 and 18, the CLI generates standalone components when you use the new application layout, but the standalone: true flag appears in the decorator of components you write. If you work on an older project, add standalone: true explicitly. In version 19 and later, the flag is the default and the CLI omits it.
The rule for a template is simple: every component, directive or pipe used in the template must be listed in the component's imports array, unless it is a built-in template feature such as @if, @for or @switch control flow, which needs no import.
The imports array in practice
The root component above imports RouterOutlet because its template contains <router-outlet />. Here is a template that uses the outlet and a child component:
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { HeaderComponent } from './header/header.component';
@Component({
selector: 'app-root',
imports: [RouterOutlet, HeaderComponent],
template: `
<app-header />
<main>
<router-outlet />
</main>
`,
})
export class AppComponent {}If you use <app-header /> but forget to add HeaderComponent to imports, the compiler reports that app-header is not a known element. The same error appears when you use a directive such as NgClass or a pipe such as DatePipe without importing it.
| Symptom | Likely cause | Fix |
|---|---|---|
| `'app-header' is not a known element` | Child component missing from `imports` | Import the class and add it to `imports` |
| `Can't bind to 'ngModel' since it isn't a known property` | `FormsModule` not imported | Add `FormsModule` to `imports` |
| `No pipe found with name 'date'` | `DatePipe` not imported | Add `DatePipe` to `imports` |
| Blank page, no template output | Selector in `index.html` differs from `selector` | Make both match |
Importing NgModules into standalone components
Standalone components can import NgModule classes. Many libraries still export modules, for example FormsModule, ReactiveFormsModule and Angular Material modules. Adding the module to imports makes everything the module exports available in the template.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-name-form',
imports: [FormsModule],
template: `
<input [(ngModel)]="name" />
<p>Hello, {{ name }}</p>
`,
})
export class NameFormComponent {
name = '';
}Imports are not inherited. A child component does not see what its parent imported. Each component lists what its own template needs. This makes files longer than in the module approach, but a component file shows every dependency it has.
Where application-wide services go
Component imports only affect templates. Services that the whole application shares go in the providers array of app.config.ts, which you saw in the bootstrapping chapter. The router, HTTP client and animations all register there, with functions such as provideRouter(routes) and provideHttpClient().
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideHttpClient } from '@angular/common/http';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes), provideHttpClient()],
};A service declared with @Injectable({ providedIn: 'root' }) needs no entry here. Angular creates it once for the whole application when something first injects it.
Template and style files
A component can keep its template and styles in separate files, referenced with templateUrl and styleUrl, or inline with template and styles. The CLI uses separate files by default. You can generate inline files with ng generate component name --inline-template --inline-style.
Component styles are scoped by default. Angular's default ViewEncapsulation.Emulated adds generated attribute selectors so that rules in app.component.css apply only to elements in that component's template. Global styles go in src/styles.css, which angular.json lists under styles.
Common errors with the root component
- Removing
RouterOutletfromimportswhile keeping<router-outlet />. The template fails to compile with an unknown element error. - Keeping unused imports. Angular 19 and later reports unused standalone imports as a compiler warning. Remove them to keep the list accurate.
- Putting logic in the root component. It is easy to grow
AppComponentinto a catch-all. Keep it to layout and the outlet, and put features in their own components. - Bootstrapping more than one root.
bootstrapApplicationis called once per page in a typical application. A second call creates a separate application with its own injector.
Checking your understanding
Exercise
A new component <app-banner /> is used in the template of AppComponent, and the build fails with "'app-banner' is not a known element". The class BannerComponent exists and is standalone. What do you change, and where?
Show answer
Import BannerComponent in the root component's file and add it to the imports array of the @Component decorator on AppComponent. Standalone components do not see each other automatically, so every component used in a template must be listed in that component's own imports.
Exercise
You rename the root component's selector from app-root to app-shell, and the page becomes blank. Which file must also change?
Show answer
src/index.html. Its <app-root></app-root> element must become <app-shell></app-shell>, because bootstrapApplication looks for an element that matches the selector.