Angular Components

A component is a piece of the interface. It has three parts: a class (data and behaviour), a template (its HTML) and metadata. The metadata is the settings…

The page of Jali's Pancake Stall will have many menu cards. If every card is written again in the App template (the HTML of App), one small change, for example to the card's border, has to be repeated on every card.

Angular splits a page into components, which are pieces of the interface. Each component has its own HTML, data and behaviour (what the component does, for example when you click it), packed into one new tag. Picture the mini pancake mould on Jali's cart. There's only one mould, but each time you pour batter in, you get one more pancake. Each pancake can get its own topping without changing the others. In Angular, the component class is the mould. Each tag of this component in a template, such as <app-menu-item> below, is one pancake from that mould. One result from the mould is called an instance (one copy of the component). Like the topping on each pancake, the data of each instance is separate.

The three parts of a component

Here is the menu card component, in its own file:

// src/app/menu-item/menu-item.ts
import { Component } from '@angular/core'

@Component({
  selector: 'app-menu-item',
  standalone: true,
  template: `
    <div class="card">
      <h3>{{ name }}</h3>
      <p>{{ '$' + price }}</p>
    </div>
  `,
  styles: `.card { border: 1px solid #ddd; border-radius: 8px; padding: 8px; }`,
})
export class MenuItem {
  name = 'Special Egg Pancake'
  price = 10
}

Using a component inside another one

The App component below uses the menu card three times:

// src/app/app.ts
import { Component } from '@angular/core'
import { MenuItem } from './menu-item/menu-item'

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [MenuItem],
  template: `
    <h1>Jali's Pancake Stall</h1>
    <app-menu-item />
    <app-menu-item />
    <app-menu-item />
  `,
})
export class App {}

That App template, line by line:

In App's template What Angular creates Shows
<h1> a plain element Jali's Pancake Stall
1st <app-menu-item /> MenuItem instance #1 a Special Egg Pancake card, $10
2nd <app-menu-item /> MenuItem instance #2 the same card
3rd <app-menu-item /> MenuItem instance #3 the same card

Without imports: [MenuItem], Angular doesn't know what <app-menu-item> is. The tag is still in the DOM, but empty, so no cards show. In the browser, the exercise editor included, the console writes this message once for each tag:

NG0304: 'app-menu-item' is not a known element (used in the 'App' component template):
1. If 'app-menu-item' is an Angular component, then verify that it is included in the '@Component.imports' of this component.

When the project is built, with ng serve or ng build, a similar message appears as compile error NG8001 (an error found during the build), and the build stops.

Standalone and NgModule

Components used to be unable to stand on their own. Every component had to be registered in an NgModule: a class with the @NgModule decorator. Its list of components is in declarations:

// Old style: required in Angular 2 to 13, still found in many older projects
@NgModule({
  declarations: [App, MenuItem],
  imports: [BrowserModule],
  bootstrap: [App],
})
export class AppModule {}

Creating a component with the CLI

ng generate component navbar   # short form: ng g c navbar

In Angular 20 and later, this command creates the folder src/app/navbar/ holding navbar.ts (class Navbar, selector app-navbar), navbar.html (the template), navbar.css, and navbar.spec.ts (for tests). Earlier versions name the file navbar.component.ts and the class NavbarComponent. The content is the same.

⚠️ Why don't the cards show up?

TL;DR: A component is a piece of the interface. It has three parts: a class (data and behaviour), a template (its HTML) and metadata. The metadata is the settings inside the @Component decorator (a mark above a class): selector, template or templateUrl, styles and imports. The selector is the component's tag name, for example <app-menu-item />. Every tag creates its own instance (a copy of the component). A component used in another component's template must be in the imports: [...] of the component that uses it, for example imports: [MenuItem] in App. If you forget, the tag is empty and error NG0304 or NG8001 appears. ng generate component navbar (short: ng g c navbar) creates a new component. Standalone (a component that stands on its own) replaces NgModule, the old way to register components. Since Angular 19 standalone is the default, in the exercise editor (Angular 22) too, so you no longer need to write standalone: true.

⚡ One component, one new tag

Write:

@Component({
  selector: 'app-menu-item',
  template: `<h3>{{ name }}</h3> <p>{{ '$' + price }}</p>`,
})
export class MenuItem {
  name = 'Special Egg Pancake'
  price = 10
}

Meaning: This is one component (a piece of the interface): the class MenuItem with the @Component decorator (a mark above a class). The class holds the data, two properties (variables that belong to the class): name and price. template is its HTML, and {{ name }} shows the value of name there. selector becomes its tag name: <app-menu-item />. Every tag creates its own instance (a copy of the component), so three tags mean three separate menu cards.

⚡ Use it in another component through imports

Write:

@Component({
  selector: 'app-root',
  imports: [MenuItem],
  template: `<app-menu-item /> <app-menu-item />`,
})
export class App {}
Guess first: the line imports: [MenuItem] is deleted. Do the cards still show?

No. Without that line, Angular doesn't know what <app-menu-item> is. The tags are still in the DOM, but empty, and the console writes error NG0304. During the build (ng build), error NG8001 appears and the build stops.

Meaning: The MenuItem component is used in the template of App, so MenuItem must be in the imports: [...] of App. The template uses the selector, <app-menu-item />, not the class name <MenuItem />.

In the exercise, <h1> must hold exactly Hyper Sheets, and <p> exactly Learning Angular is fun!.

⚡ Create a component with the CLI

Write:

ng generate component navbar   # short form: ng g c navbar

Meaning: This Angular CLI command creates a new component. In Angular 20 and later, the result is the folder src/app/navbar/ holding navbar.ts (class Navbar, selector app-navbar), navbar.html (the template), navbar.css and navbar.spec.ts (a test). Since Angular 19, a new component is standalone (usable without registering it in another file) by default.

What you will learn