Video lecture
Watch on YouTube(opens in a new tab)Sub topic 8
Building Your First Component
This chapter assumes you have a project generated with ng new, you can run it with ng serve, and you know that main.ts bootstraps a root component called App. You should also be comfortable with TypeScript classes and decorator syntax.
A component is the basic building block of an Angular UI. It pairs a TypeScript class, which holds data and behavior, with a template, which describes the HTML that Angular renders. Every screen you build is a tree of components that starts at the root App component.
The anatomy of a component
A component is a class decorated with @Component. The decorator's metadata tells Angular how to use the class:
| Property | Purpose |
|---|---|
| `selector` | The custom HTML tag that places this component in another template, such as `app-profile-card` |
| `template` or `templateUrl` | Inline HTML, or a path to an `.html` file |
| `styles` or `styleUrl` | Inline CSS, or a path to a `.css` file |
| `imports` | Other components, directives and pipes that this component's template uses |
Since Angular 19, components are standalone by default. A standalone component declares its own dependencies in imports and does not need an NgModule. Older code adds standalone: true explicitly, which still works.
Generating a component
Let the CLI create the files for you:
ng generate component profile-cardRecent CLI versions create src/app/profile-card/ with four files: profile-card.ts, profile-card.html, profile-card.css and profile-card.spec.ts. Older versions name them profile-card.component.ts and so on, and call the class ProfileCardComponent. Both conventions work the same way.
Edit the class so it holds some data:
// src/app/profile-card/profile-card.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-profile-card',
templateUrl: './profile-card.html',
styleUrl: './profile-card.css',
})
export class ProfileCard {
name = 'Ada Lovelace';
role = 'Engineer';
}Then give it a template and some styles:
<!-- src/app/profile-card/profile-card.html -->
<article class="card">
<h2>{{ name }}</h2>
<p>{{ role }}</p>
</article>/* src/app/profile-card/profile-card.css */
.card {
border: 1px solid #ccc;
border-radius: 8px;
padding: 1rem;
max-width: 240px;
}The {{ name }} syntax displays a class property in the page. The next chapter covers binding in detail.
Using the component in a parent
A component appears on screen only when another template uses its selector. Add ProfileCard to the root component's imports and place the tag in its template:
// src/app/app.ts
import { Component } from '@angular/core';
import { ProfileCard } from './profile-card/profile-card';
@Component({
selector: 'app-root',
imports: [ProfileCard],
template: `
<h1>Team</h1>
<app-profile-card />
<app-profile-card />
`,
})
export class App {}Each tag creates a separate instance of the class, so the page shows two cards.
Common errors
NG8001: 'app-profile-card' is not a known element: you used the selector but did not add the class to the parent'simportsarray.- Using
styleUrlsandstyleUrltogether:styleUrltakes one path andstyleUrlstakes an array. Pick one. - Selector typos: the tag must match
selectorexactly. Prefixes such asapp-help you avoid clashes with native HTML elements. - Missing export: the class needs
export, or the parent's import statement fails.
Exercise
You generate a Badge component with selector app-badge and put <app-badge /> in the App template, but the build fails with NG8001. What is the fix?
Show answer
Import the class in app.ts (import { Badge } from './badge/badge';) and add Badge to the imports array in App's @Component metadata, so App's template knows about the element.
Exercise
If a template contains <app-profile-card /> three times, how many ProfileCard class instances does Angular create, and do they share the name property?
Show answer
Angular creates three separate instances. Each one has its own name property, so changing one instance's value does not affect the others.