我正在尝试更改我在ion-col的*ngFor中单击的特定按钮的样式。目前,当我点击一个按钮时,所有按钮的样式都会同时改变。
以下是我的代码:
<ion-col *ngFor="let item of displayRestaurant[0].seatTimeSlotAndDiscount | slice:indexStart:indexEnd; let i=index" no-padding>
<button ion-button style="border-radius:100px;height:70px;width:70px;text-align:center;" (click)="clickedDiscount(item)" [ngClass]="clickedDiscountBoolean ? 'discountClicked' : 'discountUnclicked'">
<p style="font-size: 10px; color:white;">{{item.percentageDiscount}}</p>
<p style="font-size:10px;color:white;">{{item.timeOfDiscount}}</p>
</button>
</ion-col>SCSS:
.discountClicked{
border: 3px solid orange;
}
.discountUnclicked{
border:none;
}.ts:
clickedDiscount(discount:Discount){
this.clickedDiscountBoolean = !this.clickedDiscountBoolean;
}最初是这样的:

以下是我当前的代码在我单击任何按钮后所做的工作(新样式将应用于所有按钮):

我正在寻找的是,当我点击任何一个按钮时,只有那个按钮的样式会改变,其余的不会改变。
发布于 2019-12-14 16:55:55
所有的按钮都会查找相同的条件。您需要将该条件统一为只有一个按钮为真。您可以对此使用索引值:
<ion-col *ngFor="let item of displayRestaurant[0].seatTimeSlotAndDiscount | slice:indexStart:indexEnd; let i=index" no-padding>
<button ion-button style="border-radius:100px;height:70px;width:70px;text-align:center;"
(click)="clickedDiscount(item,i)"
[ngClass]="clickedIndex === i ? 'discountClicked' : 'discountUnclicked'">
<p style="font-size: 10px; color:white;">{{item.percentageDiscount}}</p>
<p style="font-size:10px;color:white;">{{item.timeOfDiscount}}</p>
</button>
</ion-col>然后在您的组件中
clickedDiscount(discount:Discount,index:number){
this.clickedIndex = index;
}https://stackoverflow.com/questions/59333638
复制相似问题