# @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 November 3, 2026

## 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, like `foreach` in C#.
- `track` is required: track by the item's id, so Angular updates only the items that changed. For a simple list of strings, `track $index` will do.
- `@empty` shows its block instead when the list is empty, and `$index` is 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

```html
@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:

```typescript
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' }
  ]);
}
```

```text
1. Beverages
2. Condiments
3. Confections
3 categories
```

- **`category of categories()`** names each item, like `foreach (var category in categories)`.
- **`track category.categoryId`** is 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.
- **`$index`** is the item's position, counting from 0. (`$count`, `$first` and `$last` exist too.)
- **`@empty`** is shown instead when the list is empty.

`signal<Category[]>(...)` gives the signal its type: a list of `Category`s. `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:

```html
@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.

## Key terms

- [@if](https://www.ask-codey.com/glossary/if)
- [@for](https://www.ask-codey.com/glossary/for)

## Cheat sheets

- [@if and @for](https://www.ask-codey.com/cheat-sheets/binding-and-events-in-angular-if-and-for-cheat-sheet)

Source: https://www.ask-codey.com/lessons/binding-and-events-in-angular-if-and-for
