← Back to Angular, page 1

angular-0007 · Easy fresher

How do the built-in @if, @for and @switch blocks work, and how do they compare with *ngIf and *ngFor?

@if, @for and @switch are template syntax built into the compiler since Angular 17. They replace the *ngIf, *ngFor and *ngSwitch directives, need no imports, support @else and @empty, and make track mandatory in @for.

Full answer

Angular 17 added a new block syntax for conditions and loops. It is part of the template compiler, so you import nothing.

@if (condition) { ... } @else if (other) { ... } @else { ... } shows content conditionally. You can keep the value with @if (user(); as u) { {{ u.name }} }.

@for (item of items; track item.id) { ... } @empty { ... } repeats content. Inside you get $index, $first, $last, $even, $odd, and $count. @empty renders when the list is empty.

@switch (status) { @case ('open') { ... } @default { ... } } selects one branch.

The older directives are *ngIf, *ngFor, and *ngSwitch, from CommonModule. They still work, but they are deprecated as of Angular 20 and the team plans to remove them in a later major version. Use ng generate @angular/core:control-flow to migrate a project automatically.

Why was it changed? The block syntax reads like normal code, needs no ng-template for else branches, has better type narrowing, and the @for loop is faster than *ngFor. Benchmarks published by the Angular team showed a noticeable speed gain for large lists because the diffing algorithm was reworked.

A related feature is @let name = expression; (stable in Angular 19) which declares a read-only variable inside a template.

typescript
import { Component, signal } from '@angular/core';

interface Student {
  id: number;
  name: string;
  attendance: number;
}

@Component({
  selector: 'app-attendance',
  template: `
    @if (students().length > 0) {
      <p>{{ students().length }} students</p>
    } @else {
      <p>No students yet</p>
    }

    <ul>
      @for (s of students(); track s.id; let i = $index, last = $last) {
        <li>
          {{ i + 1 }}. {{ s.name }}
          @switch (true) {
            @case (s.attendance >= 75) { <span>Eligible</span> }
            @case (s.attendance >= 60) { <span>Condonation needed</span> }
            @default { <span>Detained</span> }
          }
          @if (last) { <small>(last row)</small> }
        </li>
      } @empty {
        <li>Nothing to show</li>
      }
    </ul>
  `,
})
export class AttendanceComponent {
  students = signal<Student[]>([
    { id: 1, name: 'Asha', attendance: 82 },
    { id: 2, name: 'Imran', attendance: 66 },
    { id: 3, name: 'Meera', attendance: 54 },
  ]);
}

Angular 20.3, type-checked in strict template mode. Replace the hard-coded list with real data in a project.

Key points

  • `@if`, `@for`, `@switch` need no imports and are built into the compiler.
  • `@for` requires `track`, and has `@empty` plus `$index`-style variables.
  • `*ngIf` and `*ngFor` are legacy and deprecated from Angular 20.
  • A migration schematic converts old templates automatically.

Common mistakes

  • Writing `@for (item of items)` without `track`. This is a compile error.
  • Carrying over the old `*ngIf="x; else tpl"` pattern with an `ng-template`. In the new syntax the else branch is written as `@else { ... }` directly after the `@if` block.
  • Mixing `*ngIf` and `@if` in the same template without realising the old one still needs `NgIf` in `imports`.

Follow-ups

  • What does `track` do, and what would you track in a list from an API?
  • How does `@if (x(); as value)` help with type narrowing?
  • What is `@let` and when would you use it?

Tips

  • Say that you know the old syntax well because many projects still use it, then show the new syntax.
  • Mention the migration command. It signals real project experience.
  • If asked for the reason, talk about readability and performance, not only "it is new".

Evaluation rubric

  • Writes @if with @else, @for with track and @empty, and @switch correctly.
  • Labels *ngIf and *ngFor as legacy.
  • Knows track is required.