Using *ngSwitch for Multiple Conditions in Angular
📖 Scenario: You are building a simple Angular component that shows different messages based on a user's role. The roles can be 'admin', 'editor', or 'viewer'. You want to display a unique message for each role using Angular's *ngSwitch directive.
🎯 Goal: Create an Angular standalone component that uses *ngSwitch to display different messages for the roles 'admin', 'editor', and 'viewer'.
📋 What You'll Learn
Create a variable called userRole with the value 'editor'.
Create a variable called roles that holds the list ['admin', 'editor', 'viewer'].
Use *ngSwitch on userRole to show different messages for each role.
Add a default message for any role not in the list.
💡 Why This Matters
🌍 Real World
Many web apps need to show different content based on user roles or states. Using *ngSwitch helps manage these multiple conditions cleanly in Angular templates.
💼 Career
Understanding *ngSwitch is important for Angular developers to build dynamic user interfaces that respond to different data or user roles.
Progress0 / 4 steps
1
Set up the user role variable
Create a variable called userRole and set it to the string 'editor' inside the Angular component class.
Angular
Hint
Use userRole = 'editor'; inside the class to create the variable.
2
Add the roles list variable
Add a variable called roles that holds the array ['admin', 'editor', 'viewer'] inside the Angular component class, below userRole.
Angular
Hint
Use roles = ['admin', 'editor', 'viewer']; to create the list.
3
Use *ngSwitch to show messages for each role
In the component's template, use a div with *ngSwitch on userRole. Inside it, add *ngSwitchCase blocks for 'admin', 'editor', and 'viewer' that display the messages: 'Welcome Admin!', 'Hello Editor!', and 'Greetings Viewer!' respectively.
Angular
Hint
Use *ngSwitch="userRole" on a container and add *ngSwitchCase="'admin'", *ngSwitchCase="'editor'", and *ngSwitchCase="'viewer'" inside it.
4
Add a default message with *ngSwitchDefault
Add a *ngSwitchDefault block inside the *ngSwitch container that shows the message 'Role not recognized.' for any role not matching the cases.
Angular
Hint
Add <p *ngSwitchDefault>Role not recognized.</p> inside the *ngSwitch container.
Practice
(1/5)
1. What is the main purpose of the Angular directive *ngSwitch?
easy
A. To loop through a list of items and display them
B. To bind data from the component to the template
C. To display one template among many based on a matching value
D. To handle user input events like clicks
Solution
Step 1: Understand the role of *ngSwitch
*ngSwitch is used to conditionally display one of many templates based on a value.
Step 2: Compare with other directives
Unlike loops or event handlers, *ngSwitch chooses one template to show from multiple options.
Final Answer:
To display one template among many based on a matching value -> Option C
Quick Check:
*ngSwitch shows one option based on value = B [OK]
Hint: Remember *ngSwitch picks one view based on a value [OK]
Common Mistakes:
Confusing *ngSwitch with *ngFor for looping
Thinking *ngSwitch handles events
Using *ngSwitch for data binding only
2. Which of the following is the correct syntax to use *ngSwitchCase inside an *ngSwitch block?
A. Missing quotes around the first *ngSwitchCase value
B. Using [ngSwitch] instead of *ngSwitch
C. No *ngSwitchDefault provided
D. Using *ngSwitchCase instead of *ngSwitchWhen
Solution
Step 1: Check syntax of *ngSwitchCase values
The first case uses *ngSwitchCase=active without quotes, which is invalid for string values.
Step 2: Confirm correct usage of quotes
String values must be in quotes like *ngSwitchCase="'active'" or *ngSwitchCase="active" if active is a variable.
Final Answer:
Missing quotes around the first *ngSwitchCase value -> Option A
Quick Check:
String case values need quotes = C [OK]
Hint: Always quote string values in *ngSwitchCase [OK]
Common Mistakes:
Omitting quotes around string cases
Confusing [ngSwitch] with *ngSwitch
Forgetting *ngSwitchDefault block
5. You want to display different messages for user roles: 'admin', 'editor', 'guest', and a default for others. Which Angular template correctly uses *ngSwitch to handle this?
hard
A.
Admin Panel
Editor Dashboard
Welcome Guest
Access Denied
B.
Admin Panel
Editor Dashboard
Welcome Guest
Access Denied
C.
Admin Panel
Editor Dashboard
Welcome Guest
Access Denied
D.
Admin Panel
Editor Dashboard
Welcome Guest
Access Denied
Solution
Step 1: Check the main [ngSwitch] binding
The main switch must use property binding syntax: [ngSwitch]="userRole".
Step 2: Verify *ngSwitchCase values are strings with quotes
Each case value is a string and must be quoted like *ngSwitchCase="'admin'".
Step 3: Confirm default case is present
The default case uses *ngSwitchDefault without a value, which is correct.
Final Answer:
Correctly uses [ngSwitch] and quotes string cases -> Option D
Quick Check:
Use [ngSwitch] and quote string cases = D [OK]
Hint: Use [ngSwitch] and quote string cases in *ngSwitchCase [OK]
Common Mistakes:
Using *ngSwitch instead of [ngSwitch]
Not quoting string case values
Missing *ngSwitchDefault block
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.