Your First Angular App
Time to build something. In this lesson you replace the starter page with a small reading list app: it shows a title, a list of books and a button that adds a new one. Along the way you'll meet the four ideas every Angular app uses — a component class, a template, state held in signals and event handling.
Step 1: Clear the starter template
Open src/app/app.html, delete everything and add a heading:
// src/app/app.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.html',
})
export class App {}
// src/app/app.html
// <h1>My Reading List</h1>Save the file and the browser refreshes. That's the whole development loop: edit, save, look.
Step 2: Add state to the class
Data that the page displays lives in the component class. Wrap it in a signal so Angular knows when it changes:
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<h1>My Reading List</h1>
<p>{{ books().length }} books to read</p>
<ul>
@for (book of books(); track book) {
<li>{{ book }}</li>
}
</ul>
`,
})
export class App {
books = signal(['Dune', 'The Hobbit', 'Clean Code']);
}@for repeats a block for each item. The track expression tells Angular how to identify each item so it can update the list efficiently; for plain strings the value itself works.
Step 3: Respond to a click
Event bindings use parentheses: (click)="method()". To change a signal, call .set() or .update():
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<h1>My Reading List</h1>
<ul>
@for (book of books(); track book) {
<li>{{ book }}</li>
}
</ul>
<button (click)="addBook()">Add "Refactoring"</button>
`,
styles: `
button { background: #dd0031; color: white; border: 0; padding: 8px 14px; border-radius: 6px; }
`,
})
export class App {
books = signal(['Dune', 'The Hobbit']);
addBook() {
this.books.update((list) => [...list, 'Refactoring']);
console.log('Books now:', this.books().length);
}
}Books now: 3
The preview shows the page after one click. Notice that you never touched the DOM: you changed the data, and Angular re-rendered the list.
Note:
update()receives the current value and returns a new one. Return a new array ([...list, item]) rather than pushing into the old one, so the signal sees a different value.
Why immutable updates matter
You can try the update pattern in plain TypeScript. A function that returns a new array leaves the original untouched:
type Updater<T> = (current: T) => T;
let books: string[] = ['Dune', 'The Hobbit'];
const before = books;
const addRefactoring: Updater<string[]> = (list) => [...list, 'Refactoring'];
books = addRefactoring(books);
console.log(books);
console.log('Same array as before?', books === before);
console.log('Original still has', before.length, 'items');[ 'Dune', 'The Hobbit', 'Refactoring' ]
Same array as before? false
Original still has 2 items
What you used
| Concept | Syntax | Lesson |
|---|---|---|
| Component | @Component({ selector, template }) |
Components |
| Interpolation | {{ value }} |
Templates & Interpolation |
| Loop | @for (x of list; track x) |
Control Flow |
| Event binding | (click)="handler()" |
Event Binding |
| Reactive state | signal(), .update() |
Signals |
Test yourself
How do you read the current value of a signal named books in a template?
{{ books }}{{ books() }}{{ books.value }}{{ $books }}
Which syntax runs save() when a button is clicked?
[click]="save()"{{ click: save() }}(click)="save()"onclick="save"
Why is list => [...list, item] preferred over list.push(item) inside update()?
- push is not allowed in TypeScript
- A new array lets the signal detect that the value changed
- Spread is faster than push
- update() only accepts strings