Sub topic 3
Your first standalone component
This chapter assumes you have the project from the setup chapter and the Task interface from the typed-model chapter. It also assumes you know TypeScript classes and decorators at a basic level. You will write one component, register it in a parent, and see it render.
What "standalone" means
A component is a TypeScript class that controls one piece of the screen. Older Angular versions required every component to be declared in an NgModule, a separate class that listed what belonged together. A standalone component needs no module. It declares its own dependencies in an imports array.
Since Angular 19, components are standalone by default. In Angular 17 and 18 you must add standalone: true to the decorator. Run ng version if you are unsure which one you have.
Writing the component
Create src/app/task-list.component.ts. You can also generate it with ng generate component task-list. Newer CLI versions may name the file task-list.ts, so adjust the import paths below to match.
import { Component } from '@angular/core';
import { Task } from './task';
@Component({
selector: 'app-task-list',
template: `
<h2>My tasks</h2>
<p>Next up: {{ first.title }}</p>
`,
})
export class TaskListComponent {
readonly first: Task = { id: 1, title: 'Learn Angular', done: false };
}The @Component decorator attaches metadata to the class:
selectoris the custom HTML tag that places the component. Theapp-prefix avoids clashes with real HTML elements.templateis the HTML Angular renders. The{{ first.title }}part reads a property from the class. The next chapter covers bindings properly.- The class holds the data. Typing
firstasTaskmeans the compiler rejects a misspelled field.
Using it in the root component
A component does not become visible just because it exists. A parent must import it, and then use its selector in the parent's template. Open the root component and edit it:
import { Component } from '@angular/core';
import { TaskListComponent } from './task-list.component';
@Component({
selector: 'app-root',
imports: [TaskListComponent],
template: `
<h1>Task list</h1>
<app-task-list />
`,
})
export class AppComponent {}In Angular 20 and later, the root class is usually named App and lives in app.ts. Keep whatever name your project generated.
Run ng serve and open http://localhost:4200. You should see the heading, "My tasks", and "Next up: Learn Angular".
The entry point, main.ts, bootstraps the root component directly. No module sits in between:
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';
bootstrapApplication(AppComponent, appConfig).catch(err => console.error(err));Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `'app-task-list' is not a known element` | The component is missing from the parent's `imports` array. | Add the class to `imports`. |
| The tag shows nothing and there is no error | The selector in the template does not match the one in the decorator. | Compare both strings exactly. |
| `Can't bind to 'x' since it isn't a known property` | The child declares no such input, or the import is missing. | Check the child class and the parent's `imports`. |
| Error mentioning `standalone` on Angular 17 or 18 | The decorator lacks `standalone: true`. | Add it. |
Exercise
A teammate adds <app-task-list /> to the root template, but the browser console reports that app-task-list is not a known element. The component file exists and compiles. What is the most likely cause, and what is the fix?
Show answer
The root component does not import TaskListComponent. A standalone component is only usable in a template if the parent lists its class in the imports array of its own @Component decorator. Add imports: [TaskListComponent] to the root component, plus the matching import statement at the top of the file.