如果我单击项目,我需要添加类名,如果单击同一项目,则需要删除 ngFor 循环的类
<ion-item *ngFor="let x of statementresponse;let i=index" class="cust_delay delay" [ngClass]="{'active': selectedItem == x}" (click)="listClick($event, x)" >
</ion-item>
selectedItem:any;
listClick(event, newValue) {
console.log(newValue);
this.selectedItem = !newValue;.
}
实现此目的的方法之一是让您的物品具有"active"属性,像这样:
items = [
{name:'one', active:false},
{name:'two', active:false},
{name:'three', active:false},
];
在模板内部将它们表示如下:
<li *ngFor="let item of items"
(click)="toggleClass(item)"
[ngClass]="{'active': item.active}">{{ item.name }}</li>
最后是切换类()方法只是切换被单击项目的活动状态:
toggleClass(item){
item.active = !item.active;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)