简体   繁体   English

在 ngClass 中使用 ngFor 作用域变量作为 className 的变量

[英]Use ngFor scoped variable as variable in ngClass for className

what i'm trying to achieve is to change class dynamically using the value of the ngFor scoped variable as the class of the label.Here's my code:我想要实现的是使用 ngFor 范围变量的值作为标签的类来动态更改类。这是我的代码:

 <ul formArrayName="pokeTypes">
            <li [formGroup]="nestedForm" *ngFor="let type of this.createListOfTypes()|async; let i = index">
                <!-- add style!  -->
                <input type="checkBox" [id]='type' [value]="type" [formControlName]="type"
                    (change)="onCheckStateChange($event)">
                <label [for]="type" [ngClass]="{'{{type}}': pokeTypes.controls[0].get('type').value !== null }">{{type|titlecase}}</label>
            </li>
        </ul>
```**strong text**

Don't use ng class for this不要为此使用 ng 类

<label [for]="type" [ngClass]="{'{{type}}': pokeTypes.controls[0].get('type').value !== null }">{{type|titlecase}}</label>

Instead you can toggle your class like so:相反,您可以像这样切换您的课程:

class="{{ pokeTypes.controls[0].get('type').value !== null ? type : '' }}"

Or something similar.或类似的东西。

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

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