问题描述
实际上我想根据id减去数量。当用户单击'A'上的按钮时,只需将'A'的数量减少1。 此演示代码stackblitz
HTML
<p *ngFor="let item of arrayOfArray">
Name: {{item.name}} Quantity: {{item.quantity - currentIndex}} <button (click)="addItem(item)">minus Quantity</button>
</p>
组件
public currentIndex = 0;
arrayOfArray = [
{id: '1',name: 'A',quantity: '12'},{id: '2',name: 'B',quantity: '29'},{id: '3',name: 'C',quantity: '21'},{id: '4',name: 'D',quantity: '11'}
]
addItem(val){
if(val.id){
this.currentIndex += 1;
}
}
解决方法
您做错了。检查this demo code
用于html
<p *ngFor="let item of arrayOfArray">
Name: {{item.name}} Quantity: {{item.quantity}}
<button (click)="minusItem(item)">minus Quantity</button>
</p>
minusItem(val){
if(val.id){
for (let ele of this.arrayOfArray) {
if (ele.id === val.id){
ele.quantity--;
break
}
}
}
}