[英]individual ngClass in a ngFor loop, angular 6
I am trying to change the class of a specific position in my *ngFor, for example:我正在尝试更改 *ngFor 中特定位置的类,例如:
HTML HTML
<table>
<tr *ngFor="let article of articles; let i = index">
<td>
<input type="text" name="name" [(ngModel)]="articles[i].name" [ngClass]="{'is-valid': wrongArticle == false }" (change)="checkArticle(i)" >
</td>
</tr>
</table>
TS TS
articles = [{name: ""}, {name: ""}, {name: ""}]
wrongArticle: boolean = true;
checkArticle(i){
if(this.articles[i].name != ""){
this.wrongArticle = false;
}
}
So... my problem is... when i type something in any input... the class 'is-valid' is applied in every input... how can i apply 'is-valid' just in the correct input?所以......我的问题是......当我在任何输入中输入内容时......类'is-valid'应用于每个输入......我如何才能在正确的输入中应用'is-valid'?
Instead of defining 'wrongArticle' globally.而不是全局定义“错误的文章”。 try to use 'wrongArticle' property in the article row.
尝试在文章行中使用 'wrongArticle' 属性。 I have created a working sample application stackblitz
我已经创建了一个工作示例应用程序stackblitz
Here is the ts code这是ts代码
articles = [{name: ""}, {name: ""}, {name: ""}]
wrongArticle: boolean = true;
checkArticle(article:any){
if(article.name != ""){
article.wrongArticle = false;
}
}
here is the html code这是html代码
<table>
<tr *ngFor="let article of articles; let i = index">
<td>
<input type="text" name="name" [(ngModel)]="articles[i].name" [ngClass]="{'is-valid': article.wrongArticle == false }" (change)="checkArticle(article)" >
</td>
</tr>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.