TExcelTAD
Home › Angular › Your First Angular App

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:

angular · try it yourself
// 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:

angular · try it yourself
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():

angular · try it yourself
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:

ts · try it yourself
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?

  1. {{ books }}
  2. {{ books() }}
  3. {{ books.value }}
  4. {{ $books }}

Which syntax runs save() when a button is clicked?

  1. [click]="save()"
  2. {{ click: save() }}
  3. (click)="save()"
  4. onclick="save"

Why is list => [...list, item] preferred over list.push(item) inside update()?

  1. push is not allowed in TypeScript
  2. A new array lets the signal detect that the value changed
  3. Spread is faster than push
  4. update() only accepts strings