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> }
}