@if and @for
@if shows part of a template only when a condition is true, with @else for the other case. @for repeats part of a template for each item of a list, with track to tell the items apart, and @empty for an empty list.
Apprentice · Angular · Updated
After this lesson you can
- Show part of a template only when a condition is true, with @if and @else
- Repeat part of a template for each item of a list with @for, track and @empty
Key takeaways
@if (condition) { ... } @else { ... }puts only one of the two blocks on the page, and Angular swaps them when the data changes.@for (category of categories(); track category.categoryId)repeats its block for each item, likeforeachin C#.trackis required: track by the item's id, so Angular updates only the items that changed. For a simple list of strings,track $indexwill do.@emptyshows its block instead when the list is empty, and$indexis each item's position, counting from 0.
Templates can decide what to show, like @if and @foreach in Razor. Angular's versions are @if and @for.
@if and @else
@if (unitsInStock() > 0) {
<p class="text-success">{{ unitsInStock() }} in stock</p>
} @else {
<p class="text-danger">Out of stock</p>
}
Only one of the two paragraphs is ever on the page. When unitsInStock changes from 3 to 0, Angular takes the first one away and puts the second one in. There's also @else if (...), as in C#.
@for
@for repeats its block for each item of a list:
import { Component, signal } from '@angular/core';
interface Category {
categoryId: number;
categoryName: string;
}
@Component({
imports: [],
selector: 'app-category-list',
template: `
<ul class="list-group">
@for (category of categories(); track category.categoryId) {
<li class="list-group-item">{{ $index + 1 }}. {{ category.categoryName }}</li>
} @empty {
<li class="list-group-item">No categories yet</li>
}
</ul>
<p>{{ categories().length }} categories</p>
`,
})
export class CategoryList {
protected readonly categories = signal<Category[]>([
{ categoryId: 1, categoryName: 'Beverages' },
{ categoryId: 2, categoryName: 'Condiments' },
{ categoryId: 3, categoryName: 'Confections' }
]);
}
1. Beverages
2. Condiments
3. Confections
3 categories
category of categories()names each item, likeforeach (var category in categories).track category.categoryIdis required: it tells Angular how to recognize each item, so when the list changes, it updates only the items that changed instead of rebuilding them all. Track by the item's id; for a simple list of strings,track $index(the position) will do.$indexis the item's position, counting from 0. ($count,$firstand$lastexist too.)@emptyis shown instead when the list is empty.
signal<Category[]>(...) gives the signal its type: a list of Categorys. interface Category is a TypeScript interface describing the shape of each one, as in Interfaces in Introduction to TypeScript.
Together
@if and @for nest inside each other, and around components:
@for (product of products(); track product.productId) {
@if (!product.discontinued) {
<app-product-card />
}
}
Check yourself
2 questions to try on the lesson page.
So what
@if decides whether part of the page is there; @for repeats it for each item. With track, Angular updates lists quickly and precisely, however often they change.