Bird
Raised Fist0
Angularframework~10 mins

ngClass for dynamic classes in Angular - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to bind a CSS class dynamically using ngClass.

Angular
<div [ngClass]="'[1]'">Hello World</div>
Drag options to blanks, or click blank then click option'
Aclass
Bactive
Cstyle
Dcolor
Attempts:
3 left
💡 Hint
Common Mistakes
Using property names like 'class' or 'style' instead of class names.
Not using quotes around the class name string.
2fill in blank
medium

Complete the code to apply multiple classes conditionally using ngClass with an object.

Angular
<div [ngClass]="{ 'highlight': isActive, [1] }">Content</div>
Drag options to blanks, or click blank then click option'
A'disabled': isDisabled
B'color': isColored
C'text': isText
D'style': isStyled
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid class names or property names.
Not using quotes around class names.
3fill in blank
hard

Fix the error in the ngClass binding to correctly apply the class based on the condition.

Angular
<div [ngClass]="[1]">Example</div>
Drag options to blanks, or click blank then click option'
A"active: isActive"
B[ active: isActive ]
C( active: isActive )
D{ active: isActive }
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets or parentheses instead of curly braces.
Passing a string with colon inside instead of an object.
4fill in blank
hard

Fill both blanks to apply classes conditionally using ngClass with an object and a string.

Angular
<div [ngClass]="{ [1]: isError, '[2]' }">Message</div>
Drag options to blanks, or click blank then click option'
Aerror
Bwarning
Cinfo
Dsuccess
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around class names.
Using invalid class names.
5fill in blank
hard

Fill all three blanks to create a dynamic class binding using ngClass with an object and a string.

Angular
<div [ngClass]="{ [1]: isPrimary, [2]: isSecondary } '[3]'">Button</div>
Drag options to blanks, or click blank then click option'
Aprimary
Bsecondary
Cextra-class
Ddefault
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around class names.
Not separating the object and string properly.

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

  1. Step 1: Understand what ngClass does

    ngClass is used to control CSS classes dynamically in Angular templates.
  2. 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.
  3. Final Answer:

    To add or remove CSS classes dynamically based on component data -> Option A
  4. Quick Check:

    ngClass controls CSS classes dynamically = A [OK]
Hint: Remember: ngClass changes CSS classes dynamically [OK]
Common Mistakes:
  • Confusing ngClass with event handling
  • Thinking ngClass fetches data
  • Mixing ngClass with component creation
2. Which of the following is the correct syntax to apply multiple classes dynamically using ngClass with an array?
easy
A. <div [ngClass]="class1, class2"></div>
B. <div [ngClass]="['class1', 'class2']"></div>
C. <div [ngClass]="{'class1': true, 'class2': false}"></div>
D. <div ngClass="class1 class2"></div>

Solution

  1. Step 1: Identify array syntax for ngClass

    Using an array, classes are listed inside square brackets and quotes: ['class1', 'class2'].
  2. Step 2: Check each option's syntax

    <div [ngClass]="['class1', 'class2']"></div> correctly uses [ngClass] with an array. <div ngClass="class1 class2"></div> is plain string without binding. <div [ngClass]="{'class1': true, 'class2': false}"></div> uses object syntax, not array. <div [ngClass]="class1, class2"></div> is invalid syntax.
  3. Final Answer:

    <div [ngClass]="['class1', 'class2']"></div> -> Option B
  4. Quick Check:

    Array syntax = ['class1', 'class2'] [OK]
Hint: Array syntax uses square brackets inside [ngClass] [OK]
Common Mistakes:
  • Using string without binding for multiple classes
  • Confusing object and array syntax
  • Incorrect comma usage outside array
3. Given this component code:
isActive = true;
classes = { 'active': isActive, 'disabled': !isActive };

What classes will be applied in the template?
<div [ngClass]="classes">Content</div>
medium
A. The div will have both 'active' and 'disabled' classes
B. The div will have class 'disabled' only
C. The div will have class 'active' only
D. The div will have no classes

Solution

  1. Step 1: Evaluate the classes object values

    isActive is true, so 'active': true, and 'disabled': !true = false.
  2. Step 2: Determine which classes are applied

    Only keys with true values are applied. So only 'active' class is added.
  3. Final Answer:

    The div will have class 'active' only -> Option C
  4. Quick Check:

    True keys apply classes = A [OK]
Hint: Only true keys in object apply classes [OK]
Common Mistakes:
  • Assuming false keys add classes
  • Confusing boolean values with strings
  • Thinking all keys apply regardless of value
4. Identify the error in this Angular template snippet:
<div [ngClass]="{active: isActive, disabled: !isActive}">Test</div>

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

  1. Step 1: Check object syntax in ngClass binding

    Angular allows unquoted property names in object literals inside templates.
  2. Step 2: Verify boolean usage and binding syntax

    Boolean values are valid to conditionally apply classes. Square brackets are required for binding.
  3. Final Answer:

    The syntax is correct; no error -> Option D
  4. 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

  1. Step 1: Analyze the condition for each class

    'highlight' applies if score > 80, 'low-score' applies if score <= 80.
  2. 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.
  3. Final Answer:

    <div [ngClass]="{highlight: score > 80, 'low-score': score <= 80}"></div> -> Option A
  4. Quick Check:

    Object with correct conditions = C [OK]
Hint: Use object syntax with conditions for multiple exclusive classes [OK]
Common Mistakes:
  • Applying both classes always with array syntax
  • Reversing condition logic
  • Forgetting property binding [ngClass] for dynamic expressions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard