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
}
- The class (
export class MenuItem) holds data and behaviour. Here the data is two properties (variables that belong to the class):nameandprice. - The template is the component's HTML.
{{ name }}shows the value of the class'snameproperty. The next lesson covers how this works in full. - The metadata is the object inside
@Component({...}).@Componentis a decorator that says the class is a component, and the object inside it holds the component's settings:selector: the tag name for using this component,<app-menu-item>. It usually starts withapp-and has a hyphen, so its name is never the same as a built-in HTML tag.template(HTML written right here) ortemplateUrl: './menu-item.html'(HTML in a separate file, the default in a project made by the CLI).styles(CSS written right here) orstyleUrl(CSS in a separate file). This CSS applies only inside this component, so.cardhere doesn't touch elements in other components.standaloneandimports: covered below.
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 {}
import { MenuItem } from ...at the top is a plain TypeScript import. It takes theMenuItemclass from its file, so this file can use it.imports: [MenuItem]inside@Componenttells the template ofAppthat the<app-menu-item>tag is theMenuItemcomponent. Both are needed:importfor the TypeScript file,importsfor the template.- Three tags mean three instances: Angular creates three separate
MenuItemobjects. Changing data in one card doesn't change the other two cards. - For now all three cards are the same, because the value of
nameis fixed in the class. Giving each card its own name comes in the Input and Output lesson.
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 {}
- Angular 14 introduced standalone components, and Angular 15 made them stable (officially ready to use). Since Angular 17,
ng newcreates standalone projects. - Since Angular 19,
standalone: trueis the default, song generateno longer writes that line in new components. An old-style component that still belongs to an NgModule is markedstandalone: false. - The exercise editor here runs Angular 22. So a component without
standalone: truestill runs, and the exercise doesn't ask you to write that line. You'll still often see that line in Angular 14 to 18 code.
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?
- Forgetting
imports. The tag is there but empty, and the console writes NG0304 (at build time: NG8001). Import the class at the top of the file, then add it toimports: [...]. - Writing the class name as a tag. In the template you write the selector,
<app-menu-item />, not<MenuItem />. - Writing
standalone: falseon the main component. Angular stops withNG0907: The AppComponent component is not marked as standalone, but Angular expects to have a standalone component here.Since Angular 19, just delete that line: components are standalone by default. - Slightly different text. The exercise checks that
<h1>holds exactlyHyper Sheetsand<p>holds exactlyLearning Angular is fun!.
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.