DThe div will have both 'active' and 'error' classes.
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
intermediate
2:00remaining
Identify the correct ngClass syntax for multiple classes
Which option correctly applies multiple classes 'btn' and 'btn-primary' using ngClass in Angular?
A[ngClass]="{'btn': true, 'btn-primary': true}"
B[ngClass]="{'btn btn-primary': true}"
C[ngClass]="['btn', 'btn-primary']"
D[ngClass]="('btn', 'btn-primary')"
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
advanced
2:00remaining
Why does this ngClass binding not apply the class?
Consider this Angular template:
<div [ngClass]="{ active: isActive }">Hello</div>
and component code:
isActive = '';
Why does the 'active' class not get applied?
ABecause ngClass only accepts arrays, not objects.
BBecause isActive is a string '', not a boolean true.
CBecause the class name 'active' must be in quotes.
DBecause the div element cannot have dynamic classes.
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.
What classes does the div have after clicking the button twice?
AThe div has class 'error'.
BThe div has no classes.
CThe div has both 'error' and 'normal' classes.
DThe div has class 'normal'.
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
expert
2: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?
ANo classes will be applied because null and undefined are treated as false.
BBoth 'highlight' and 'disabled' classes will be applied.
COnly 'highlight' class will be applied because null is truthy.
DOnly 'disabled' class will be applied because undefined is truthy.
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.
Practice
(1/5)
1. What is the main purpose of ngClass in Angular?
easy
A. To add or remove CSS classes dynamically based on component data
B. To fetch data from a server
C. To create new Angular components
D. To handle user input events
Solution
Step 1: Understand what ngClass does
ngClass is used to control CSS classes dynamically in Angular templates.
Step 2: Compare options with ngClass purpose
Only To add or remove CSS classes dynamically based on component data describes adding or removing CSS classes dynamically, which matches ngClass functionality.
Final Answer:
To add or remove CSS classes dynamically based on component data -> Option A
Assuming isActive is a boolean property in the component.
medium
A. Property names in object must be quoted strings
B. Boolean values cannot be used in ngClass object
C. Square brackets should be removed from ngClass
D. The syntax is correct; no error
Solution
Step 1: Check object syntax in ngClass binding
Angular allows unquoted property names in object literals inside templates.
Step 2: Verify boolean usage and binding syntax
Boolean values are valid to conditionally apply classes. Square brackets are required for binding.
Final Answer:
The syntax is correct; no error -> Option D
Quick Check:
Object syntax with booleans is valid = B [OK]
Hint: Object keys can be unquoted in Angular templates [OK]
Common Mistakes:
Thinking object keys must be quoted strings
Removing square brackets breaks binding
Believing booleans are invalid in ngClass
5. You want to apply the class 'highlight' only when score is above 80 and 'low-score' when it is below or equal to 80. Which ngClass binding correctly implements this logic?
hard
A. <div [ngClass]="{highlight: score > 80, 'low-score': score <= 80}"></div>
B. <div ngClass="score > 80 ? 'highlight' : 'low-score'"></div>
C. <div [ngClass]="['highlight', 'low-score']"></div>
D. <div [ngClass]="{highlight: score <= 80, 'low-score': score > 80}"></div>
Solution
Step 1: Analyze the condition for each class
'highlight' applies if score > 80, 'low-score' applies if score <= 80.
Step 2: Check each option's logic
<div [ngClass]="{highlight: score > 80, 'low-score': score <= 80}"></div> correctly maps conditions to classes in an object. <div ngClass="score > 80 ? 'highlight' : 'low-score'"></div> lacks property binding [ngClass], so the ternary is not evaluated (static string). <div [ngClass]="['highlight', 'low-score']"></div> applies both classes always. <div [ngClass]="{highlight: score <= 80, 'low-score': score > 80}"></div> reverses conditions incorrectly.