ngClass is a directive in Angular that lets you add or remove CSS classes dynamically based on component data or expressions.
Click to reveal answer
intermediate
How do you apply multiple classes conditionally using ngClass?
<p>You can pass an object to <code>ngClass</code> where keys are class names and values are conditions. Classes with true conditions get applied.</p><pre><code><div [ngClass]="{ 'class1': isActive, 'class2': hasError }"></div></code></pre>
Click to reveal answer
beginner
Can ngClass accept an array? How does it work?
<p>Yes. You can pass an array of class names to <code>ngClass</code>. All classes in the array will be added to the element.</p><pre><code><div [ngClass]="['class1', 'class2']"></div></code></pre>
Click to reveal answer
intermediate
What happens if the condition in ngClass changes at runtime?
Angular automatically updates the classes on the element to reflect the new condition. Classes are added or removed without page reload.
Click to reveal answer
beginner
Show a simple example of <code>ngClass</code> toggling a class based on a boolean variable <code>isActive</code>.
Clicking the button adds or removes active-class on the div.
Click to reveal answer
What type of value can ngClass NOT accept?
AString with space-separated class names
BNumber representing class count
CObject with class names as keys and booleans as values
DArray of class names
✗ Incorrect
ngClass accepts strings, arrays, or objects but not numbers.
How does Angular update classes when the condition in ngClass changes?
AIgnores changes after initial render
BManually requires developer to update classes
CReloads the whole page
DAutomatically adds or removes classes based on new conditions
✗ Incorrect
Angular automatically updates classes dynamically without page reload.
Which syntax correctly applies multiple classes conditionally with ngClass?
AAll of the above
B[ngClass]="['class1', 'class2']"
C[ngClass]="'class1 class2'"
D[ngClass]="{ 'class1': true, 'class2': false }"
✗ Incorrect
All these syntaxes are valid ways to use ngClass.
What is the benefit of using ngClass over static class attributes?
AAllows dynamic styling based on component state
BMakes HTML code longer
CPrevents CSS from loading
DDisables user interaction
✗ Incorrect
ngClass helps change styles dynamically as data changes.
If you want to add a class only when a variable hasError is true, which is correct?
A[ngClass]="['error', hasError]"
B[ngClass]="{ hasError: 'error' }"
C[ngClass]="{ 'error': hasError }"
D[ngClass]="hasError ? 'error' : ''"
✗ Incorrect
Object syntax with class name as key and condition as value is correct.
Explain how ngClass helps in applying CSS classes dynamically in Angular components.
Think about how classes change when variables change in your component.
You got /4 concepts.
Describe three different ways to use ngClass to add classes to an element.
Consider the types of values you can bind to <code>ngClass</code>.
You got /3 concepts.
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.