Sub topic 8
Handling empty collections with @empty
This chapter covers the @empty block that you attach to an @for block. It assumes you know TypeScript, Angular components and templates, and property binding. It also assumes you have seen @for with a track expression. Signals are helpful but not required.
Why @empty exists
A list that has no items renders nothing. The user sees a blank area and cannot tell whether the data is still loading, failed, or is truly empty. You normally want a message such as "No tasks yet." instead.
Before built-in control flow, you wrote this with a second condition, like *ngIf="items.length === 0", next to the *ngFor. @empty removes that second check. It belongs to the @for block, so the loop and its fallback sit together in one place.
Basic syntax
Put @empty directly after the closing brace of the @for block. It runs when the collection has no items.
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; let i = $index, isEven = $even) {
<li [class.striped]="isEven">{{ i + 1 }}. {{ task.title }}</li>
} @empty {
<li>No tasks yet.</li>
}
</ul>
<button (click)="tasks.set([])">Clear all</button>
`,
})
export class TaskListComponent {
tasks = signal<Task[]>([
{ id: 1, title: 'Write spec' },
{ id: 2, title: 'Review pull request' },
]);
}Click "Clear all" and the two <li> items are removed. The @empty content takes their place. Set the signal back to a non-empty array and the fallback is removed again.
What counts as empty
The @empty block renders when the iterated collection has zero items. That includes an empty array and an empty iterable such as an empty Set. In current Angular versions, null and undefined also produce no iterations and show the @empty content. Check this against the version you target, because it is easy to rely on by accident.
The distinction matters for loading states. If your data starts as undefined while a request is in flight, @empty can show "No tasks yet." before the data arrives. Guard against that with an outer @if.
import { Component, signal } from '@angular/core';
interface Task {
id: number;
title: string;
}
@Component({
selector: 'app-guarded-list',
template: `
@if (tasks(); as list) {
<ul>
@for (task of list; track task.id) {
<li>{{ task.title }}</li>
} @empty {
<li>No tasks yet.</li>
}
</ul>
} @else {
<p>Loading tasks...</p>
}
`,
})
export class GuardedListComponent {
tasks = signal<Task[] | undefined>(undefined);
}An empty array is truthy in JavaScript, so @if (tasks(); as list) passes for []. The @empty block handles that case. Only undefined falls to the loading branch.
Rules for the block
@emptymust follow the@forblock immediately. Nothing else can sit between them.- It takes no expression and no parameters.
- The implicit variables
$index,$first, and the others are not available inside it, because there is no current item. - You can put any template content in it, including components, other control flow blocks, and bindings.
- It can only be paired with
@for. An@ifblock uses@elsefor its fallback.
The decision flow looks like this:
Common mistakes
- Placing markup between
}and@empty. Angular reports a template parse error, because@emptymust be attached directly to the loop. - Using
$indexinside@empty. It is only defined within the loop body. - Showing "No results" while data is still loading. Separate the loading state from the empty state, as in the guarded example.
- Putting the wrapper element inside the fallback by habit. In the list example,
<ul>stays outside the block, so the fallback<li>remains valid HTML.
Exercises
Exercise
A component holds orders = signal<Order[] | undefined>(undefined) and loads data from a server. The template uses @for (o of orders(); track o.id) { ... } @empty { <p>No orders.</p> }. Users briefly see "No orders." on page load. Why, and how do you fix it?
Show answer
A @for over undefined produces no iterations, so the @empty block renders while the request is still pending. Wrap the loop in @if (orders(); as list) { @for (o of list; track o.id) { ... } @empty { <p>No orders.</p> } } @else { <p>Loading...</p> }. An empty array is truthy, so a loaded but empty result reaches @empty, and undefined shows the loading branch.
Exercise
Why does this template fail to compile: @for (t of tasks(); track t.id) { <li>{{ t.title }}</li> } <li>None</li> @empty { <li>None</li> }?
Show answer
The @empty block must directly follow the closing brace of the @for block. The extra <li>None</li> between them breaks the pairing, so remove it.