AI Hints
Home›Angular Tutorial›@if, @else and @else if and @for
Angular · Lesson 39 of 120

@for

Repeat for every item.

⏱ 2 min read▶ Video at 47:13🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

A bookshop shows many books. We don't write the same HTML five times. At for repeats it, once for every book.

  • Many books
  • Write it once
  • @for repeats it

The @for block

At for, book, of books, repeats its content once for each item. Inside, book is the current item. Track tells Angular how to tell items apart, usually by id. And track is required.

keyvalue
@for (book of books; ...)repeat for each
bookthe current item
track book.idhow to tell them apart
trackrequired

Listing books

Books comes from a shared file, with five books, each with an id, title, author, price, and stock. At for repeats the list item, for every book. Inside, book dot title, and the others, show the current book. Run it. All five books appear, each on its own line.

app.tstypescript
import { Component } from '@angular/core';
import { BOOKS } from './books';

@Component({
  selector: 'app-root',
  template: `
    <h2>Our books</h2>
    <ul>
      @for (book of books; track book.id) {
        <li>{{ book.title }} by {{ book.author }} · £{{ book.price }}</li>
      }
    </ul>
  `,
})
export class App {
  books = BOOKS;
}
Also uses books.ts
export interface Book {
  id: number;
  title: string;
  author: string;
  price: number;
  stock: number;
}

export const BOOKS: Book[] = [
  { id: 1, title: 'Dune', author: 'Frank Herbert', price: 9.99, stock: 4 },
  { id: 2, title: 'The Hobbit', author: 'J.R.R. Tolkien', price: 8.99, stock: 0 },
  { id: 3, title: 'Circe', author: 'Madeline Miller', price: 10.99, stock: 2 },
  { id: 4, title: 'Emma', author: 'Jane Austen', price: 6.99, stock: 7 },
  { id: 5, title: 'Persuasion', author: 'Jane Austen', price: 7.49, stock: 1 },
];

Index and last

At for gives us extra values. Dollar index is the position, starting at zero. And dollar last is true for the last item. We store them in i, and last, with let. Run it. The books are numbered from one, and the last is marked.

app.tstypescript
import { Component } from '@angular/core';
import { BOOKS } from './books';

@Component({
  selector: 'app-root',
  template: `
    @for (book of books; track book.id; let i = $index, last = $last) {
      <p>{{ i + 1 }}. {{ book.title }} {{ last ? '(the last one)' : '' }}</p>
    }
    <p>{{ books.length }} books in total</p>
  `,
})
export class App {
  books = BOOKS;
}
Also uses books.ts
export interface Book {
  id: number;
  title: string;
  author: string;
  price: number;
  stock: number;
}

export const BOOKS: Book[] = [
  { id: 1, title: 'Dune', author: 'Frank Herbert', price: 9.99, stock: 4 },
  { id: 2, title: 'The Hobbit', author: 'J.R.R. Tolkien', price: 8.99, stock: 0 },
  { id: 3, title: 'Circe', author: 'Madeline Miller', price: 10.99, stock: 2 },
  { id: 4, title: 'Emma', author: 'Jane Austen', price: 6.99, stock: 7 },
  { id: 5, title: 'Persuasion', author: 'Jane Austen', price: 7.49, stock: 1 },
];

Summary

At for repeats a block for every item, with track to tell items apart, and extras like dollar index. Next, more about track.

Key takeaways
  • @for (b of books; track b.id)
  • $index
  • $last
Next up: track — telling items apart