Challenge - 5 Problems
ngClass Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does ngClass apply classes dynamically?
Given this Angular template snippet, what classes will be applied to the
when
isActive is true and hasError is false?<div [ngClass]="{ 'active': isActive, 'error': hasError }">Content</div>Attempts:
2 left
💡 Hint
Remember ngClass adds classes where the condition is true.
✗ Incorrect
ngClass applies classes whose conditions evaluate to true. Since isActive is true, 'active' is added. hasError is false, so 'error' is not added.
📝 Syntax
intermediate2:00remaining
Identify the correct ngClass syntax for multiple classes
Which option correctly applies multiple classes
'btn' and 'btn-primary' using ngClass in Angular?Attempts:
2 left
💡 Hint
ngClass accepts arrays or objects with boolean values.
✗ Incorrect
Option A uses an object with each class as a key and true as value, which is valid. Option A is valid syntax and also correct. Option A treats two classes as one string key which is invalid. Option A is invalid syntax.
🔧 Debug
advanced2:00remaining
Why does this ngClass binding not apply the class?
Consider this Angular template:
and component code:
Why does the 'active' class not get applied?
<div [ngClass]="{ active: isActive }">Hello</div>and component code:
isActive = '';
Why does the 'active' class not get applied?
Attempts:
2 left
💡 Hint
Check the type of the condition value.
✗ Incorrect
ngClass applies classes whose conditions evaluate to truthy values. A string '' is falsy in JavaScript, so it does not apply the class.
❓ state_output
advanced2:00remaining
What classes are applied after toggling state?
Given this component code:
and template:
What classes does the div have after clicking the button twice?
isError = false;
toggle() { this.isError = !this.isError; }
and template:
<button (click)="toggle()">Toggle</button>
<div [ngClass]="{ 'error': isError, 'normal': !isError }">Status</div>
What classes does the div have after clicking the button twice?
Attempts:
2 left
💡 Hint
Each click toggles isError between true and false.
✗ Incorrect
Initially isError is false, so 'normal' is applied. After first click, isError is true, so 'error' applies. After second click, isError is false again, so 'normal' applies.
🧠 Conceptual
expert2:00remaining
How does ngClass handle null or undefined values in class bindings?
If you use ngClass with an object like
{ 'highlight': isHighlighted, 'disabled': isDisabled } and isHighlighted is null and isDisabled is undefined, what classes will Angular apply?Attempts:
2 left
💡 Hint
Think about how JavaScript treats null and undefined in boolean contexts.
✗ Incorrect
In JavaScript, null and undefined are falsy values. ngClass treats falsy values as false, so neither class is applied.