mr_blond
mr_blond

Reputation: 1730

How to use conditional class and class with variable with ngClass in Angular?

There is two classes should be applied to the same div:

<div [ngClass]="['class_01_'+someVar, {'class_02': isSelected}]><div>

I get the error:

Error: NgClass can only toggle CSS classes expressed as strings, got [object Object]

How to use conditional class and class with variable with ngClass in Angular?

Upvotes: 0

Views: 1360

Answers (1)

bryan60
bryan60

Reputation: 29355

you can't really do it with ngClass alone given what you're trying to do. You'll need to do it with separate ngClass and class directives like:

<div [ngClass]="'class_01_'+someVar" [class.class_02]="isSelected"><div>

Upvotes: 3

Related Questions