Welcome back Angular devs! ๐
In Part 1 and Part 2, we explored the powerful @if and @for blocks in Angular 20โs new control flow system.
Now, letโs complete the trio with the newly introduced @switch directive โ a clean and readable way to handle multiple conditional views.
๐ What is @switch?
The @switch block allows Angular templates to conditionally render one of several possible blocks, based on the result of a single expression โ just like JavaScriptโs switch statement.
๐ง Syntax
@switch (expression) {
@case (value1) {
<!-- Template for case value1 -->
}
@case (value2) {
<!-- Template for case value2 -->
}
@default {
<!-- Fallback template if no case matches -->
}
}
๐งช Example: Status Viewer
Letโs build a simple component that shows the status of a request and lets users update it with the click of a button.
<div class="status">
@switch (status()) {
@case ('Init') {
<span>Request Init</span>
}
@case ('Loading') {
<span>Request In Progress</span>
}
@case ('Completed') {
<span>Request Completed</span>
}
@default {
<span>Status not available</span>
}
}
</div>
<div class="btn-container">
<button type="button" (click)="requestChange('Init')">Init Request</button>
<button type="button" (click)="requestChange('Loading')">Request In Progress</button>
<button type="button" (click)="requestChange('Completed')">Request Completed</button>
</div>
๐ฏ Component Code
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-status',
templateUrl: './status.html',
styleUrl: './status.scss'
})
export class StatusComponent {
status = signal<'Init'| 'Loading' | 'Completed'| ''>(''); // Starts as empty
requestChange(value: string) {
this.status.set(value);
}
}
Output:
On component load, the default message is shown (Status not available).
Clicking a button updates the status dynamically, and the matching case content is rendered โ all without cluttering your component with conditionals.
๐ Notes:
-
@switchcompares using strict equality (===). -
@defaultis optional. - Thereโs no fallthrough like in JS. Each case is isolated โ so no need for
breakorreturn.
โ
Summary of @switch
| Feature | Behavior |
|---|---|
| Strict Compare | Uses === to match values |
| No Fallthrough | Each @case is isolated |
| Optional Default |
@default is a fallback but not mandatory |
| Clean Syntax | Clear, nested, and familiar structure |
๐งต Final Words
With @if, @for, and now @switch, Angular 20 templates just got a huge upgrade in readability, reactivity, and structure.
Missed previous parts?
โก๏ธ Part 1: @if
โก๏ธ Part 2: @for
โ๏ธ Author: Vetriselvan
๐จโ๐ป Frontend Developer | Code Lover | Exploring Angularโs future

Top comments (0)