Skip to main content

Lab 12: Standalone & Control Flow

πŸ“– Resources​

πŸš€ Starter Code​

step-12-album-wholesale-v21-post-migration

In this lab, you'll modernize your templates by replacing Angular's structural directives with the new control flow syntax. You'll convert *ngIf to @if/@else, *ngFor to @for with track, and [ngSwitch] to @switch/@case.

πŸ“ Instructions​

Step 1: Replace *ngIf with @if​

<!-- Before -->
<div *ngIf="loading">Loading...</div>

<!-- After -->
@if (loading) {
<div>Loading...</div>
}

With else:

@if (loading) {
<div>Loading...</div>
} @else {
<div>Content</div>
}

Step 2: Replace *ngFor with @for​

<!-- Before -->
<div *ngFor="let album of albums; trackBy: trackByFn">
{{ album.name }}
</div>

<!-- After -->
@for (album of albums; track album.id) {
<div>{{ album.name }}</div>
}

Step 3: Replace [ngSwitch] with @switch​

<!-- Before -->
<div [ngSwitch]="status">
<span *ngSwitchCase="'active'">Active</span>
<span *ngSwitchDefault>Inactive</span>
</div>

<!-- After -->
@switch (status) {
@case ('active') { <span>Active</span> }
@default { <span>Inactive</span> }
}