简体   繁体   English

动态类绑定在 angular 9 中不起作用

[英]dynamic class binding not working in angular 9

I need to display dynamic color for a div based on some condition.我需要根据某些条件为 div 显示动态颜色。 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学生是我的数组,我在 css 中有一个名为 .clr-5、clr-6 等的类

try this:尝试这个:

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

class bindings are updated in Angular 9. Read more about it here类绑定在 Angular 9 中更新。在此处阅读更多相关信息

string interpolation not work inside property binding.字符串插值在属性绑定中不起作用。 you should deal with that like:你应该像这样处理:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM