htmlcssangularng-class

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?


Solution

  • 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>