Build your first Angular appPart 5 of 6
Your first component: one counter, used three times
Everything you have built so far lives in one component, App. That works for a counter, and stops working the moment the page grows. This part moves the counter into a component of its own, gives it a label from the outside, and puts three of them on the page - each one counting on its own.
By Adrien Colombier
What you'll have at the end
- A
Countercomponent generated by the CLI, holding the code from part four - The same component used several times, each copy with its own count
- A label passed in from the parent with
input() - A list of counters rendered with
@for, and why it needstrack
What you need
- Part four finished: the click counter working in
app.tsandapp.html ng serverunning in a terminal- About 10 minutes
Step 1
Let the CLI write the component
A component is a class, a template and a stylesheet - exactly the three files App is made of. You could create them by hand; the CLI does it in one line. In a second terminal, from the project folder:
$ ng generate component counterCREATE src/app/counter/counter.css (0 bytes)CREATE src/app/counter/counter.spec.ts (539 bytes)CREATE src/app/counter/counter.ts (189 bytes)CREATE src/app/counter/counter.html (22 bytes)Four files in a new src/app/counter/ folder. The .spec.ts is a test, which this series does not cover - you can leave it alone. The other three are a component with nothing in it yet:
$ cat src/app/counter/counter.tsimport { Component } from '@angular/core'; @Component({ imports: [], selector: 'app-counter', styleUrl: './counter.css', templateUrl: './counter.html',})export class Counter {} $ cat src/app/counter/counter.html<p>counter works!</p>selector: 'app-counter'is the tag you will write in a template to use it. Theapp-prefix is the CLI's default, and it stops your tags from ever colliding with a real HTML element.export class Counteris the name you will import it by. NoComponentsuffix - that is the current Angular naming style, the same oneAppfollows.
Step 2
Put it on the page
Replace the counter markup in app.html with the new tag:
<h1>Built by me, with {{ title() }}</h1> <app-counter />Save, and the build fails:
❯ Changes detected. Rebuilding...✔ Changes detected. Rebuilding...Application bundle generation failed. [0.081 seconds] - 2026-11-02T14:50:05.180Z ✘ [ERROR] NG8001: 'app-counter' is not a known element:1. If 'app-counter' is an Angular component, then verify that it is included in the '@Component.imports' of this component.2. If 'app-counter' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@Component.schemas' of this component to suppress this message. Find more at https://next.angular.dev/errors/NG8001 [plugin angular-compiler] /home/you/my-first-angular-app/src/app/app.html:3:0: 3 │ <app-counter /> ╵ ~~~~~~~~~~~~~~~ Error occurs in the template of component App. /home/you/my-first-angular-app/src/app/app.ts:6:15: 6 │ templateUrl: './app.html', ╵ ~~~~~~~~~~~~This is the other side of the rule from part four: each component lists what its template uses. App has never heard of app-counter. Import the class and add it to imports - the error's first suggestion, word for word:
import { Component, signal } from '@angular/core';import { Counter } from './counter/counter'; @Component({ imports: [Counter], selector: 'app-root', styleUrl: './app.css', templateUrl: './app.html',})export class App { protected readonly title = signal('Angular');}Note that App no longer has count, doubled or the two methods - cut them rather than delete them, because they move into Counter next. The page now shows counter works! under your heading.
Step 3
Move the counter into it
Take what you wrote in part four and give it to Counter. The class gets the signals and the two methods, plus one new line - label, which comes next:
import { Component, computed, input, signal } from '@angular/core'; @Component({ imports: [], selector: 'app-counter', styleUrl: './counter.css', templateUrl: './counter.html',})export class Counter { readonly label = input('Clicks'); protected readonly count = signal(0); protected readonly doubled = computed(() => this.count() * 2); protected increment() { this.count.update((value) => value + 1); } protected reset() { this.count.set(0); }}<h2>{{ label() }}</h2> <p>You clicked {{ count() }} times. Doubled, that is {{ doubled() }}.</p> <button (click)="increment()">Click me</button><button (click)="reset()">Reset</button> @if (count() >= 10) { <p>Ten clicks. You can stop now.</p>}The page is back to a working counter, now with a Clicks heading on top. It behaves exactly as it did in part four - the difference is that it is now a thing you can reuse.
Step 4
Pass it a label from the outside
input('Clicks') declares an input: a value the parent can give to the component, with 'Clicks' as the default when it does not. Inside the component it is read like any signal - label() - but it is read-only there: only the parent sets it.
Use the counter twice, with a label each:
<h1>Built by me, with {{ title() }}</h1> <app-counter label="Coffees" /><app-counter label="Bugs fixed" />❯ Changes detected. Rebuilding...✔ Changes detected. Rebuilding...Initial chunk files | Names | Raw sizemain.js | main | 6.77 kB | Application bundle generation complete. [0.125 seconds] - 2026-11-02T14:50:20.236Z Component update sent to client(s).Two counters. Click one and the other does not move: each <app-counter> on the page is its own instance of the class, with its own count signal. And because you only touched a template, the terminal says Component update - no reload.
Step 5
Repeat it over a list, with @for
Writing one tag per counter is fine for two. For a list that could change, keep the labels in the class and let the template loop over them:
export class App { protected readonly title = signal('Angular');+ protected readonly counters = ['Coffees', 'Bugs fixed', 'Commits']; }<h1>Built by me, with {{ title() }}</h1> @for (name of counters; track name) { <app-counter [label]="name" />}Two new things in that template.
[label]="name"with square brackets passes the value of the expressionname. Without brackets,label="name"would pass the literal textnameto all three counters. Brackets for an expression, no brackets for fixed text.track nametells Angular how to recognise each item from one render to the next, so when the list changes it can keep, move or remove the right counter instead of rebuilding all of them. Here the labels are unique, so the label itself is a fine key.
track is not optional. Leave it out and the build stops:
❯ Changes detected. Rebuilding...✔ Changes detected. Rebuilding...Application bundle generation failed. [0.039 seconds] - 2026-11-02T14:50:40.113Z ✘ [ERROR] NG5002: @for loop must have a "track" expression [plugin angular-compiler] /home/you/my-first-angular-app/src/app/app.html:3:0: 3 │ @for (name of counters) { ╵ ~~~~~~~~~~~~~~~~~~~~~~~~~ Error occurs in the template of component App. /home/you/my-first-angular-app/src/app/app.ts:8:15: 8 │ templateUrl: './app.html', ╵ ~~~~~~~~~~~~That is the shape of every Angular app: small components, each owning its own state, assembled by their parents and configured through inputs. A product card, a menu item or a comment is a Counter with a different template.
So far, though, it is all one page. Part six adds a second one with the router - and brings back the RouterOutlet you removed in part four.
Next in this series
Part 6 of 6
Add a second page with the router
Routes, router-outlet and routerLink, unknown URLs sent home, the one mistake Angular does not catch, and what lazy loading really does to the build.
Add a second page with the router