Sub topic 5
Rendering lists with @for
This chapter assumes you can write an Angular standalone component, bind values in a template with interpolation and property binding, and read a signal by calling it, as in tasks(). The @for block repeats a piece of template once for each item in a collection. It replaces *ngFor and needs no import.
Basic syntax
A @for block has three parts: a loop variable, the collection, and a required track expression.
import { Component, signal } from '@angular/core';
interface Task {
id: number;
title: string;
}
@Component({
selector: 'app-task-list',
template: `
<ul>
@for (task of tasks(); track task.id) {
<li>{{ task.title }}</li>
}
</ul>
`,
})
export class TaskListComponent {
tasks = signal<Task[]>([
{ id: 1, title: 'Write spec' },
{ id: 2, title: 'Review pull request' },
]);
}Here task is the loop variable. It is visible only inside the braces. tasks() is the collection, which can be an array or any other iterable, such as a Set or a Map. The block's content renders once per item, in order.
The @for block does not use a leading * and does not wrap its content in an extra element. The <li> elements above are direct children of the <ul>.
Why track is required
When the collection changes, Angular compares the old and new items to decide which DOM nodes to keep, move, create, or destroy. The track expression produces the identity of each item for that comparison.
- If an item's identity appears in both the old and new collections, Angular reuses its DOM nodes and updates the bindings.
- If an identity is new, Angular creates nodes.
- If an identity is gone, Angular destroys its nodes.
With a stable identity such as a database id, reordering a list moves existing nodes instead of rebuilding them. That keeps input focus, scroll position, and animation state on the right rows.
Choosing the expression
| Expression | Behavior | Use when |
|---|---|---|
| `track item.id` | Identity follows the item | Items have a unique, stable key |
| `track $index` | Identity follows the position | The list is static or only appended to |
| `track item` | Identity follows the object reference | Items are primitives, or the same objects persist between updates |
Tracking by $index is legal but has a cost. If you insert an item at the top, every position holds a different item. Angular then updates every row's bindings instead of adding one row.
Implicit variables and @empty
Inside the block, Angular provides $index, $count, $first, $last, $even, and $odd. To use one under a shorter name, alias it in the @for header with let. An optional @empty block renders when the collection has no items.
import { Component, signal } from '@angular/core';
interface Task {
id: number;
title: string;
}
@Component({
selector: 'app-numbered-tasks',
template: `
<ul>
@for (task of tasks(); track task.id; let i = $index, isEven = $even) {
<li [class.striped]="isEven">{{ i + 1 }}. {{ task.title }}</li>
} @empty {
<li>No tasks yet.</li>
}
</ul>
`,
})
export class NumberedTasksComponent {
tasks = signal<Task[]>([]);
}Later chapters cover these variables and @empty in detail. For now, note that the aliases go after track, separated by semicolons.
Common errors
- Missing
track. The compiler reports an error. Unlike*ngFor, there is no default. - Duplicate track values. If two items produce the same identity, Angular warns in development mode, and updates can behave unpredictably. Make sure your key is unique.
- Using
let item of items. That is*ngForsyntax. In@for, writeitem of items. - Passing
nullorundefined. These render as an empty list, so@emptyruns. A non-iterable value, such as a plain object, throws an error.
Exercise
A list of users is reloaded from an API every 30 seconds, and each reload returns new objects with the same id values. Which track expression keeps existing DOM nodes, and why does track user not do that?
Show answer
Use track user.id. The id values stay the same across reloads, so Angular matches old and new items and reuses their nodes. With track user, each reload produces new object references, so every identity looks new and Angular destroys and recreates all the rows.