Sub topic 12
Rendering lists efficiently with track
This chapter assumes you can build a FormArray of FormGroups, bind it with formArrayName and [formGroupName], and add and remove rows. It also assumes you know Angular's built-in @for block. Here you will see how the track expression affects the DOM, focus, and performance of a dynamic form.
Why @for requires track
When the array changes, @for has to decide which existing DOM rows to keep, move, create, or destroy. The track expression gives each item an identity. If an item has the same identity before and after the change, Angular reuses its DOM. If not, Angular destroys the old row and builds a new one.
For a FormArray, each entry is a control object. That object is a stable identity. FormArray.controls is a plain array, so tracking by the control itself works well:
import { Component, inject } from '@angular/core';
import { NonNullableFormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
@Component({
selector: 'app-tag-list',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="form">
<div formArrayName="tags">
@for (tag of tags.controls; track tag; let i = $index) {
<div [formGroupName]="i">
<input formControlName="label" />
<button type="button" (click)="remove(i)">Remove</button>
</div>
}
</div>
<button type="button" (click)="add()">Add tag</button>
</form>
`,
})
export class TagListComponent {
private fb = inject(NonNullableFormBuilder);
form = this.fb.group({ tags: this.fb.array([this.create('first'), this.create('second')]) });
get tags() {
return this.form.controls.tags;
}
create(label = '') {
return this.fb.group({ label: [label, Validators.required] });
}
add() {
this.tags.push(this.create());
}
remove(index: number) {
this.tags.removeAt(index);
}
}Click into the second input, then remove the first row. The second row keeps its DOM, cursor, and focus, because its control object is unchanged.
Choosing a track expression
| Expression | Identity | Result for a FormArray |
|---|---|---|
| `track item` | The control object | Best default. Rows follow their controls when you remove or reorder. |
| `track $index` | Position | Works, but after a removal every later row is re-bound to a different control. Reordering is also unreliable. |
| `track item.value.id` | A field inside the value | Fine if the field is a stable, unique id that the user never edits. |
Do not track by an editable field such as name. Each keystroke changes the identity, so Angular destroys and recreates the row, and the input loses focus.
Common mistakes
- Creating controls during rendering. If a getter or template call returns new controls on every change detection pass, the identities change each time and the whole list is rebuilt. Create controls only in event handlers or setup code, then read them through a stable reference such as
this.form.controls.tags. - Clearing and refilling. After
clear()followed bypush(), every control is new, so every row is rebuilt. This is expected when loading data. Avoid doing it on each change event. - Using
trackwith a duplicate key. If two rows share a tracked value, Angular warns and reuses DOM incorrectly. Control objects never collide, which is another reason to prefer them.
Exercise
A form lists 50 order lines with track $index. The user removes line 3, and the inputs of lines 4 to 50 flicker as Angular updates their bindings. What single change to the @for line fixes this, and why does it work?
Show answer
Change it to track item, where item is the control from items.controls. Each control object keeps its identity when line 3 is removed, so Angular keeps the DOM rows for lines 4 to 50 and destroys only the row for line 3.