2023-11-01 01:02:22 +00:00
The `@for` block repeatedly renders content of a block for each item in a collection.
2024-07-17 00:33:19 +00:00
## Syntax
2023-11-01 01:02:22 +00:00
2024-07-22 15:37:24 +00:00
```angular-html
2023-11-01 01:02:22 +00:00
@for (item of items; track item.name) {
2024-01-05 09:04:31 +00:00
< li > {{ item.name }}< / li >
2023-11-01 01:02:22 +00:00
} @empty {
2024-01-05 09:04:31 +00:00
< li > There are no items.< / li >
2023-11-01 01:02:22 +00:00
}
```
2024-07-17 00:33:19 +00:00
## Description
2023-11-01 01:02:22 +00:00
The `@for` block renders its content in response to changes in a collection. Collections can be any
JavaScript [iterable ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols ),
but there are performance advantages of using a regular `Array` .
You can optionally include an `@empty` section immediately after the `@for` block content. The
content of the `@empty` block displays when there are no items.
2025-01-15 16:23:12 +00:00
Angular's `@for` block does not support flow-modifying statements like JavaScript's `continue` or `break` .
2024-07-17 00:33:19 +00:00
### `track` and objects identity
2023-11-01 01:02:22 +00:00
The value of the `track` expression determines a key used to associate array items with the views in
the DOM. Having clear indication of the item identity allows Angular to execute a minimal set of DOM
operations as items are added, removed or moved in a collection.
2024-01-05 09:04:31 +00:00
To optimize performance, especially in loops over immutable data, ensure the track expression is effectively used to
identify each item uniquely. Because of the potential for poor performance, the `track` expression
is required for the `@for` loops.
For collections that remain static , `track $index` provides a straightforward tracking mechanism. For dynamic
collections experiencing additions, deletions, or reordering, opt for a
unique property of each item as the tracking key.
2023-11-01 01:02:22 +00:00
2025-05-09 20:53:58 +00:00
Track expressions can only reference `$index` , the item, and fields from the component class. If the `let` segment of the `@for` block introduced an alias for `$index` , that alias may also be referenced.
2024-07-17 00:33:19 +00:00
### `$index` and other contextual variables
2023-11-01 01:02:22 +00:00
2024-01-05 09:04:31 +00:00
Inside `@for` contents, several implicit variables are always available:
2023-11-01 01:02:22 +00:00
2024-01-05 09:04:31 +00:00
| Variable | Meaning |
2025-11-06 18:03:05 +00:00
| -------- | --------------------------------------------- |
2023-11-01 01:02:22 +00:00
| `$count` | Number of items in a collection iterated over |
2024-01-05 09:04:31 +00:00
| `$index` | Index of the current row |
| `$first` | Whether the current row is the first row |
| `$last` | Whether the current row is the last row |
| `$even` | Whether the current row index is even |
| `$odd` | Whether the current row index is odd |
2023-11-01 01:02:22 +00:00
These variables are always available with these names, but can be aliased via a `let` segment:
2024-07-22 15:37:24 +00:00
```angular-html
2023-11-01 01:02:22 +00:00
@for (item of items; track item.id; let idx = $index, e = $even) {
2024-01-05 09:04:31 +00:00
Item #{{ idx }}: {{ item.name }}
2023-11-01 01:02:22 +00:00
}
```
The aliasing is especially useful in case of using nested `@for` blocks where contextual variable
names could collide.