Sub topic 4
Rendering the list with bindings and @for
This chapter assumes you have the Angular CLI project from the setup chapter, the Task interface from the modeling chapter, and a standalone component. It also assumes Angular 17 or later, which has the built-in @for syntax. Here you will render an array of tasks with template bindings and control flow.
How data reaches the template
A template is the HTML of a component. Angular evaluates expressions in it against the component class, so any class property or method is available by name.
The four bindings you need
| Syntax | Name | Direction | Use |
|---|---|---|---|
| `{{ task.title }}` | Interpolation | Class to template | Show text |
| `[checked]="task.done"` | Property binding | Class to template | Set a DOM property |
| `[class.done]="task.done"` | Class binding | Class to template | Toggle one CSS class |
| `(click)="remove(task.id)"` | Event binding | Template to class | Run a method on an event |
Square brackets set a property on the element. Parentheses listen for an event. Both are checked against your types when the app compiles.
Rendering with @for
@for repeats a block once per item in a collection. It needs a track expression, which tells Angular how to identify each item. When the array changes, Angular uses the tracked value to reuse existing DOM nodes instead of rebuilding them all. Use a stable unique value such as task.id.
The optional @empty block renders when the collection has no items.
The component below keeps tasks in a plain array for now. Later chapters move this state into signals and a service.
import { Component } from '@angular/core';
import { Task } from './task';
@Component({
selector: 'app-task-list',
template: `
<ul>
@for (task of tasks; track task.id) {
<li>
<input type="checkbox" [checked]="task.done" (change)="toggle(task.id)" />
<span [class.done]="task.done">{{ task.title }}</span>
<button type="button" (click)="remove(task.id)">Delete</button>
</li>
} @empty {
<li>No tasks yet.</li>
}
</ul>
<p>{{ remaining }} remaining</p>
`,
styles: `.done { text-decoration: line-through; }`,
})
export class TaskListComponent {
tasks: Task[] = [
{ id: 1, title: 'Install the Angular CLI', done: true },
{ id: 2, title: 'Render a list', done: false },
];
get remaining(): number {
return this.tasks.filter(t => !t.done).length;
}
toggle(id: number): void {
this.tasks = this.tasks.map(t => (t.id === id ? { ...t, done: !t.done } : t));
}
remove(id: number): void {
this.tasks = this.tasks.filter(t => t.id !== id);
}
}Add <app-task-list /> to your root component's template, and add TaskListComponent to that component's imports array. Then run ng serve. You should see two rows, and the count should update when you check a box.
Both methods build a new array instead of editing the old one. Replacing the array keeps old and new states distinct, and it is the habit the signals chapter relies on.
Common errors
- Missing
track.@forwithout atrackexpression fails to compile. The error message names the missing expression. - Tracking by index or by object.
track $indexworks, but items that move or get deleted lose their DOM state, such as a half-typed input. Tracking bytask.idavoids this. - Duplicate ids. Two items with the same tracked value produce a runtime warning and unpredictable reuse. Ids must be unique within the list.
- Using
[checked]where you meantchecked. Without brackets,checked="task.done"sets the literal string, which is always truthy. Use brackets whenever the value is an expression. - Forgetting the import. A child component used in a template but missing from
importscauses an "is not a known element" error.
Exercise
In the template above, why does [class.done]="task.done" strike through only the completed tasks, and what would you change to also show the row number starting at 1?
Show answer
The class binding adds the done class only when task.done is true and removes it when false, so the CSS rule applies only to completed tasks. To show a row number, alias the index with let i = $index in the @for line and render {{ i + 1 }} in the row.