Control Flow
Learn how Angular uses control flow to conditionally display, repeat, and switch between different template sections.
What is Control Flow?
Angular control flow determines which parts of a template should be displayed based on conditions, collections, or values.
Modern Angular provides built-in control flow blocks such as
@if, @for, and @switch
for creating dynamic user interfaces.
Control Flow
Data
Component provides the data used by the template.
Condition
Angular evaluates a condition or collection.
Control Block
Angular selects the required template content.
Rendered UI
The selected content appears on the page.
Main Control Flow Concepts
@if
Displays content when a condition is true.
@else
Displays alternative content when the condition is false.
@for
Repeats template content for each item in a collection.
@switch
Selects content based on different possible values.
@if Control Flow
The @if block displays template content only when
the specified condition evaluates to true.
@if (isLoggedIn) {
<p>Welcome Sam!</p>
}
Welcome Sam!
@if with @else
Use @else when you want to display alternative
content when the condition is false.
@if (isLoggedIn) {
<p>Welcome Sam!</p>
} @else {
<p>Please login to continue.</p>
}
Condition True
Welcome Sam!
Condition False
Please login
@else if
Multiple conditions can be checked using
@else if.
@if (marks >= 75) {
<p>Grade: A</p>
} @else if (marks >= 60) {
<p>Grade: B</p>
} @else {
<p>Grade: C</p>
}
Grade: A
@for Control Flow
The @for block repeats template content for every
item in a collection.
@for (student of students; track student.id) {
<p>
{{ student.name }}
</p>
}
Sam
Pradnya
Rahul
Tracking Items with @for
The track expression helps Angular identify each item
when the collection changes.
@for (student of students; track student.id) {
<div>
<strong>
{{ student.name }}
</strong>
</div>
}
Using a stable identifier such as student.id
helps Angular efficiently update the rendered list.
@empty Block
The @empty block provides alternative content when
the collection used by @for contains no items.
@for (student of students; track student.id) {
<p>
{{ student.name }}
</p>
} @empty {
<p>No students found.</p>
}
No students found.
@switch Control Flow
The @switch block is useful when different UI
content should be displayed for different values.
@switch (course) {
@case ('Angular') {
<p>Angular Full Stack</p>
}
@case ('Java') {
<p>Java Full Stack</p>
}
@case ('Dot Net') {
<p>.NET Full Stack</p>
}
@default {
<p>Course not available.</p>
}
}
Angular Full Stack
Switch Flow
Value
course
Case
Angular
Template
Angular Full Stack
Traditional vs Modern Control Flow
| Purpose | Traditional Angular | Modern Angular |
|---|---|---|
| Conditional rendering | *ngIf |
@if |
| Alternative content | ng-template |
@else |
| Looping | *ngFor |
@for |
| Empty collection | Separate condition | @empty |
| Multiple cases | ngSwitch |
@switch |
Common Uses of Control Flow
Conditional UI
Show or hide content based on application state.
Lists
Display multiple records from a collection.
Empty States
Display useful content when no records are available.
Multiple Cases
Display different UI based on a selected value.
Complete Control Flow Example
@if (students.length > 0) {
<h3>Students</h3>
@for (student of students; track student.id) {
<div>
<h4>
{{ student.name }}
</h4>
<p>
Course: {{ student.course }}
</p>
</div>
}
} @else {
<p>No students found.</p>
}
Student Dashboard
Student: Sam
Course: Angular Full Stack
Student: Pradnya
Course: Java Full Stack
Best Practices
Avoid putting complex business logic directly inside control flow expressions.
Use @if, @for, and
@switch when building modern Angular
applications.
Use a stable unique identifier such as
student.id when rendering collections.
Use @empty to provide meaningful feedback
when there are no records.
Avoid deeply nested control flow and move complex logic into components or services.
Summary
@ifis used for conditional rendering.@elseprovides alternative content.@forrepeats content for collection items.@emptyhandles an empty collection.@switchhandles multiple possible cases.- The
trackexpression helps Angular identify list items efficiently. - Control flow makes Angular templates dynamic and data-driven.