Explain it simply
Attribute: ngClass, in plain language
ngClass directive is used to dynamically add or remove CSS classes on an element.
Problem → Why → Solution
The problem — why this concept matters
Concatenating many conditional class strings in a template invites typos and inconsistent styling.
A common mistake
One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.
Core concept — understand how it works
Class binding expresses visual state through semantic CSS classes. Choose [class.name] or an ngClass object, array, or string according to the condition's complexity, while keeping visual policy in CSS. Map dynamic states through an allowlist rather than constructing class names directly from user text.
- input and state
- component and template
- rendered Angular UI
Explain the core concept and Angular data flow of Attribute: ngClass
Core syntax or pattern
<!-- Object syntax -->
<div [ngClass]="{'active': isActive, 'disabled': isDisabled, 'highlight': true}">
Dynamic Classes
</div>
<!-- Array syntax -->
<div [ngClass]="['btn', 'btn-primary', isLarge ? 'btn-lg' : 'btn-sm']">
Button
</div>
<!-- String syntax -->
<div [ngClass]="currentTheme">
Themed Content
</div>
// component.ts
export class MyComponent {
isActive = true;
isDisabled = false;
isLarge = true;
currentTheme = 'dark-theme';
}Class binding expresses visual state through semantic CSS classes. Choose [class.name] or an ngClass object, array, or string according to the condition's complexity, while keeping visual policy in CSS. Map dynamic states through an allowlist rather than constructing class names directly from user text.
Read the code step by step
- input and state
<!-- Object syntax -->ngClass directive is used to dynamically add or remove CSS classes on an element.
- component and template
<div [ngClass]="currentTheme">Class binding expresses visual state through semantic CSS classes. Choose [class.name] or an ngClass object, array, or string according to the condition's complexity, while keeping visual policy in CSS. Map dynamic states through an allowlist rather than constructing class names directly from user text.
- rendered Angular UI
}Implement a status badge with a domain-state-to-class map and test active, disabled, error, and unknown states.
- input and state
- component and template
- rendered Angular UI
Success
Explain the core concept and Angular data flow of Attribute: ngClass
Failure
Concatenating many conditional class strings in a template invites typos and inconsistent styling.
ngClass directive is used to dynamically add or remove CSS classes on an element.
<!-- Object syntax -->
<div [ngClass]="{'active': isActive, 'disabled': isDisabled, 'highlight': true}">
Dynamic Classes
</div>
<!-- Array syntax -->
<div [ngClass]="['btn', 'btn-primary', isLarge ? 'btn-lg' : 'btn-sm']">
Button
</div>
<!-- String syntax -->
<div [ngClass]="currentTheme">
Themed Content
</div>
// component.ts
export class MyComponent {
isActive = true;
isDisabled = false;
isLarge = true;
currentTheme = 'dark-theme';
}(The 'active' and 'highlight' classes will be applied to the first div)Real-world usage
How to apply it in a real project
Implement a status badge with a domain-state-to-class map and test active, disabled, error, and unknown states.
Mini challenge
Build it yourself
Rebuild the Attribute: ngClass example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
<!-- Object syntax -->
<div [ngClass]="{'active': isActive, 'disabled': isDisabled, 'highlight': true}">
Dynamic Classes
</div>
<!-- Array syntax -->
<div [ngClass]="['btn', 'btn-primary', isLarge ? 'btn-lg' : 'btn-sm']">
Button
</div>
<!-- String syntax -->
<div [ngClass]="currentTheme">
Themed Content
</div>
// component.ts
export class MyComponent {
isActive = true;
isDisabled = false;
isLarge = true;
currentTheme = 'dark-theme';
}Expected: Explain the core concept and Angular data flow of Attribute: ngClass
Show hint
Implement a status badge with a domain-state-to-class map and test active, disabled, error, and unknown states.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- input and state → component and template → rendered Angular UI
- Class binding expresses visual state through semantic CSS classes. Choose [class.name] or an ngClass object, array, or string according to the condition's complexity, while keeping visual policy in CSS. Map dynamic states through an allowlist rather than constructing class names directly from user text.
- Concatenating many conditional class strings in a template invites typos and inconsistent styling.
- Implement a status badge with a domain-state-to-class map and test active, disabled, error, and unknown states.
Angular — Templates — Angular