mevr
mevr

Reputation: 1125

dynamic class binding not working in angular 9

I need to display dynamic color for a div based on some condition. I am getting console error.

I have tried

<div [ngClass]="{'clr-{{students.rollNo+1}}': students.active}"></div>

students is my array, i have a class called .clr-5, clr-6 etc... in css

Upvotes: 0

Views: 704

Answers (2)

Tanvee Singh
Tanvee Singh

Reputation: 11

string interpolation not work inside property binding. you should deal with that like:

<div [ngClass]="{`clr-${students.rollNo+1}`: students.active}"></div>

Upvotes: 1

Noor Ul Ain
Noor Ul Ain

Reputation: 600

try this:

<div [ngClass]="[ students.active ? 'clr-'+students.rollNo+1 : '']"></div>

class bindings are updated in Angular 9. Read more about it here

Upvotes: 1

Related Questions