Sub topic 11
Nesting and combining blocks
This chapter shows how to put @if, @for, and @switch inside each other. It assumes you know basic TypeScript, Angular components and templates, and the syntax of each block from the earlier chapters. Signals are used in the examples, but observables work the same way.
Blocks are ordinary template content
Each block's body is regular template content. You can place any element, binding, or other block inside it. No block needs a special wrapper, and none needs an import. Because the blocks use braces, the nesting is easy to read when you indent consistently.
Three combinations come up most often:
- An
@ifguarding an@for, so a list renders only when data exists. - An
@forcontaining an@ifor@switch, so each item picks its own template. - An
@emptyblock holding an@if, so the empty message depends on a second condition.
An @if around an @for
Use an outer @if when the whole list area needs a different layout, such as a heading or a toolbar that should appear only when the data has loaded.
import { Component, signal } from '@angular/core';
interface Order {
id: number;
label: string;
status: 'pending' | 'shipped' | 'cancelled';
}
@Component({
selector: 'app-orders',
template: `
@if (loaded()) {
<h2>Orders</h2>
<ul>
@for (order of orders(); track order.id) {
<li>
{{ order.label }}:
@switch (order.status) {
@case ('pending') { <em>Waiting to ship</em> }
@case ('shipped') { <strong>On its way</strong> }
@default { <span>Cancelled</span> }
}
</li>
} @empty {
@if (filter()) {
<li>No orders match "{{ filter() }}".</li>
} @else {
<li>You have no orders yet.</li>
}
}
</ul>
} @else {
<p>Loading orders...</p>
}
`,
})
export class OrdersComponent {
loaded = signal(true);
filter = signal('');
orders = signal<Order[]>([
{ id: 1, label: 'Keyboard', status: 'shipped' },
{ id: 2, label: 'Monitor', status: 'pending' },
{ id: 3, label: 'Cable', status: 'cancelled' },
]);
}Four blocks work together here. The outer @if and @else handle loading. The @for renders the rows. Each row runs its own @switch on order.status. If the list is empty, the @empty block runs a second @if to choose between two messages.
How the pieces interact
Variables from an outer block stay visible inside inner blocks. In the example, the @switch reads order, which the @for declared. The implicit variables of @for ($index, $first, and the others) belong to the loop that declares them. When you nest two @for blocks, an inner reference to $index means the inner loop. To reach the outer index, alias it in the outer loop.
import { Component, signal } from '@angular/core';
interface Group {
id: number;
name: string;
members: { id: number; name: string }[];
}
@Component({
selector: 'app-groups',
template: `
@for (group of groups(); track group.id; let groupIndex = $index) {
<h3>{{ groupIndex + 1 }}. {{ group.name }}</h3>
<ol>
@for (member of group.members; track member.id) {
<li>{{ groupIndex + 1 }}.{{ $index + 1 }} {{ member.name }}</li>
}
</ol>
}
`,
})
export class GroupsComponent {
groups = signal<Group[]>([
{ id: 1, name: 'Design', members: [{ id: 10, name: 'Mia' }, { id: 11, name: 'Noor' }] },
{ id: 2, name: 'Platform', members: [{ id: 20, name: 'Sam' }] },
]);
}The outer loop aliases $index as groupIndex. Inside the inner loop, $index is the member position and groupIndex is the group position. Each @for needs its own track expression, and each one should use a stable id.
Choosing between combinations
| Goal | Combination |
|---|---|
| Show a list only after loading | `@if` outside, `@for` inside |
| Vary each row by a field | `@switch` or `@if` inside `@for` |
| Two different empty messages | `@if` inside `@empty` |
| Several states, one value | `@switch`, not a chain of `@if` blocks |
Keeping nested templates readable
Deep nesting is legal, but each level adds indentation and makes the template harder to scan. Two habits help:
- Prefer
@else ifchains over nested@ifblocks when the conditions are mutually exclusive. - Move a deeply nested row into its own child component and pass the item as an input.
Exercise
In a template with an outer @for (team of teams(); track team.id) and an inner @for (player of team.players; track player.id), how can you print the team's position inside the inner loop?
Show answer
Alias the outer index on the outer loop, for example @for (team of teams(); track team.id; let teamIndex = $index). Inside the inner loop, $index refers to the player position, so use teamIndex to show the team position.
Exercise
You want a list to show "No results for your search" when a search term is set and "Nothing here yet" otherwise, both when the list is empty. Which blocks do you use, and where?
Show answer
Use @for with an @empty block. Inside @empty, place an @if (searchTerm()) with the first message and an @else with the second.