猿问

根据 id 减去数量

实际上我想根据 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;

    }

  }


阿晨1998
浏览 131回答 1
1回答

qq_花开花谢_0

你做错了 。检查这个演示代码对于 html<p *ngFor="let item of arrayOfArray">&nbsp;&nbsp; Name: {{item.name}} Quantity: {{item.quantity}}&nbsp; <button (click)="minusItem(item)">minus Quantity</button></p>&nbsp; minusItem(val){&nbsp; &nbsp; if(val.id){&nbsp; &nbsp; &nbsp; for (let ele of this.arrayOfArray) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (ele.id === val.id){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ele.quantity--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答