0
0
Angularframework~20 mins

*ngSwitch for multiple conditions in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ngSwitch Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will be rendered by this Angular template using *ngSwitch?

Consider this Angular template snippet:

<div [ngSwitch]="status">
  <p *ngSwitchCase="'loading'">Loading...</p>
  <p *ngSwitchCase="'success'">Data loaded successfully!</p>
  <p *ngSwitchCase="'error'">Error occurred.</p>
  <p *ngSwitchDefault>Unknown status.</p>
</div>

If the component's status property is set to 'success', what will be displayed?

ALoading...
BError occurred.
CData loaded successfully!
DUnknown status.
Attempts:
2 left
💡 Hint

Look at which *ngSwitchCase matches the status value.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses *ngSwitch with multiple cases for the same output?

You want to display the same message for both 'pending' and 'in-progress' statuses using *ngSwitch. Which template snippet is correct?

A
&lt;pre&gt;&amp;lt;div [ngSwitch]="status"&amp;gt;
  &amp;lt;p *ngSwitchCase="'pending'"&amp;gt;Work in progress...&amp;lt;/p&amp;gt;
  &amp;lt;p *ngSwitchCase="'in-progress'"&amp;gt;Work in progress...&amp;lt;/p&amp;gt;
  &amp;lt;p *ngSwitchDefault&amp;gt;Other status&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;&lt;/pre&gt;
B
&lt;pre&gt;&amp;lt;div [ngSwitch]="status"&amp;gt;
  &amp;lt;p *ngSwitchCase="'pending' || 'in-progress'"&amp;gt;Work in progress...&amp;lt;/p&amp;gt;
  &amp;lt;p *ngSwitchDefault&amp;gt;Other status&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;&lt;/pre&gt;
C
&lt;pre&gt;&amp;lt;div [ngSwitch]="status"&amp;gt;
  &amp;lt;p *ngSwitchCase="['pending', 'in-progress']"&amp;gt;Work in progress...&amp;lt;/p&amp;gt;
  &amp;lt;p *ngSwitchDefault&amp;gt;Other status&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;&lt;/pre&gt;
D
&lt;pre&gt;&amp;lt;div [ngSwitch]="status"&amp;gt;
  &amp;lt;p *ngSwitchCase="'pending' &amp;amp;&amp;amp; 'in-progress'"&amp;gt;Work in progress...&amp;lt;/p&amp;gt;
  &amp;lt;p *ngSwitchDefault&amp;gt;Other status&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;&lt;/pre&gt;
Attempts:
2 left
💡 Hint

Remember that *ngSwitchCase accepts one value per case.

state_output
advanced
2:30remaining
What is the rendered output when using *ngSwitch with nested conditions?

Given this Angular template and component code:

<div [ngSwitch]="user.role">
  <div *ngSwitchCase="'admin'">
    <span *ngSwitch="user.status">
      <em *ngSwitchCase="'active'">Admin Active</em>
      <em *ngSwitchCase="'inactive'">Admin Inactive</em>
      <em *ngSwitchDefault>Admin Unknown</em>
    </span>
  </div>
  <div *ngSwitchCase="'guest'">Guest User</div>
  <div *ngSwitchDefault>Unknown Role</div>
</div>

And the component has user = { role: 'admin', status: 'inactive' }. What will be displayed?

AAdmin Active
BUnknown Role
CGuest User
DAdmin Inactive
Attempts:
2 left
💡 Hint

Check the outer *ngSwitchCase first, then the inner one.

🔧 Debug
advanced
2:30remaining
Why does this *ngSwitch template not render the expected case?

Examine this Angular template:

<div [ngSwitch]="color">
  <p *ngSwitchCase="red">Red color</p>
  <p *ngSwitchCase="blue">Blue color</p>
  <p *ngSwitchDefault>Unknown color</p>
</div>

If the component's color property is set to 'red' (string), why does it always show 'Unknown color'?

ABecause the [ngSwitch] directive requires a number, not a string.
BBecause the *ngSwitchCase values are missing quotes, so they are treated as variables, not strings.
CBecause *ngSwitchDefault always overrides other cases.
DBecause the color property is not initialized in the component.
Attempts:
2 left
💡 Hint

Check how string literals are written in Angular templates.

🧠 Conceptual
expert
3:00remaining
How does Angular's *ngSwitch handle multiple matching cases?

Consider an Angular template with multiple *ngSwitchCase directives that could match the same value due to overlapping expressions. What is Angular's behavior in this scenario?

AAngular renders only the first matching *ngSwitchCase and ignores the rest.
BAngular renders all matching *ngSwitchCase blocks simultaneously.
CAngular throws a runtime error due to multiple matches.
DAngular renders the last matching *ngSwitchCase and ignores the others.
Attempts:
2 left
💡 Hint

Think about how switch statements work in programming languages.