0

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

2 Answers 2

1

try this:

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

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

Sign up to request clarification or add additional context in comments.

6 Comments

calculation not working clr-'+students.rollNo+1 need to add 1 with rollno
It will get concatenated with your dynamic value of roll number, here is an example: codesandbox.io/s/small-feather-0vk1z?file=/src/app/…
sorry cannot view this, white screen i can see!
if 25 is roll no then I need to add 25+1=26 should be the value not concatenation (251) Thanks!
you can try with creating a method that returns your calculated value and then concatenate with class like this: 'clr-'+ addRollNumber(rollNo) the method definition: addRollNumber(rollNo) { return rollNo + 1; }
|
1

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

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

1 Comment

calculation not working clr-'+students.rollNo+1 need to add 1 with rollno

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.