Sub topic 7
Using $index, $first, $last, and the other implicit variables
This chapter assumes you know TypeScript, Angular components and templates, and how to use signals in a template. You should also know how @for and track work. Here you will learn the six variables that @for provides inside its block and how to use them.
What the implicit variables are
Inside an @for block, Angular makes six variables available without any declaration. Each one describes the current iteration.
| Variable | Type | Meaning |
|---|---|---|
| `$index` | number | Zero-based position of the current item |
| `$count` | number | Total number of items in the collection |
| `$first` | boolean | `true` for the first item |
| `$last` | boolean | `true` for the last item |
| `$even` | boolean | `true` when `$index` is even |
| `$odd` | boolean | `true` when `$index` is odd |
You can use them directly, for example {{ $index }}. Because $index starts at 0, add 1 when you show a human-readable number.
Aliasing with let
Use let after the track expression to give a variable a local name. Separate several aliases with commas. This is useful for readability. It is required when you nest @for blocks, because an inner $index hides the outer one.
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>
`,
})
export class TaskListComponent {
tasks = signal<Task[]>([
{ id: 1, title: 'Write spec' },
{ id: 2, title: 'Review pull request' },
]);
}The alias i holds $index, and isEven holds $even. The [class.striped] binding adds a CSS class to every other row.
You do not have to alias a variable. Writing $even directly inside the block works just as well.
Using $first, $last, and $count
$first and $last suit separators and special styling for the ends of a list. $count suits summaries.
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-breadcrumbs',
template: `
<nav>
@for (part of parts(); track part) {
<span [class.current]="$last">{{ part }}</span>
@if (!$last) {
<span> / </span>
}
}
<small>({{ parts().length }} levels)</small>
</nav>
`,
})
export class BreadcrumbsComponent {
parts = signal(['Home', 'Docs', 'Control flow']);
}The separator / appears after every item except the last. The @if inside the loop reads $last from the enclosing @for, because the variables are visible in nested blocks.
Inside the loop, $count gives the same number as parts().length. Use whichever reads better. $count avoids calling the signal again.
Nested loops and shadowing
Each @for defines its own set of variables. When you nest loops, alias the outer ones so the inner block can still reach them.
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-grid',
template: `
@for (row of rows(); track $index; let r = $index) {
<div>
@for (cell of row; track $index; let c = $index) {
<span>({{ r }}, {{ c }}): {{ cell }}</span>
}
</div>
}
`,
})
export class GridComponent {
rows = signal([
['a', 'b'],
['c', 'd'],
]);
}Without the aliases, $index in the inner block would always refer to the column. Here track $index is acceptable only because these static strings have no stable identity of their own. For data with an id, track by the id.
Common mistakes
- Treating
$indexas one-based. It starts at 0. - Using
$indexintrackfor lists that are reordered or filtered. Angular then reuses DOM nodes by position, not by identity, which can mix up component state. - Forgetting to alias in nested loops, so the inner
$indexhides the outer one. - Trying to assign to these variables. They are read-only.
Exercise
You render a list with @for (item of items(); track item.id). You want to show "Item 3 of 5" for the third item, and add a "Last" badge on the final one. Which variables do you use, and why do you add 1?
Show answer
Use $index and $count for the text, written as Item {{ $index + 1 }} of {{ $count }}, and use $last in an @if (!$last) or @if ($last) check for the badge. You add 1 because $index is zero-based, so the third item has an index of 2.
Exercise
In a nested @for, the inner block needs the position of the current outer item. How do you make it available?
Show answer
Alias it in the outer loop, for example let r = $index. The inner loop's own $index hides the outer one, but the alias r remains readable.