Angular 19 Control Flow Syntax: @if, @for, and @switch Explained
Angular 19 Control Flow Syntax: @if, @for, and @switch Explained
@if (users.length > 0) {
@for (user of users; track user.id) {
<p>{{ user.name }}</p>
} @empty {
<p>No users found.</p>
}
} @else {
<p>Loading...</p>
}Angular 19 introduced a new built-in control flow syntax that replaces the traditional structural directives like *ngIf, *ngFor, and *ngSwitch. The new syntax uses @if, @for, and @switch blocks directly in templates.
Why the change?
- Better performance with optimized rendering
- Cleaner template syntax
- Built-in empty state handling with @empty
- No need to import CommonModule
Example:
@if (users.length > 0) {
@for (user of users; track user.id) {
<p>{{ user.name }}</p>
} @empty {
<p>No users found.</p>
}
} @else {
<p>Loading...</p>
}