Maureen Moore

Maureen Moore

  • NA
  • 206
  • 0

Angular is updating all fields simultaneously when it should just upda

Jul 6 2020 10:11 AM
I have a *ngFor and I'm trying to update a number field on click but it updates all of the items with same value.
In my html:
  1. <form *ngFor="let product of products" [formGroup]="myForm" name="myForm" (ngSubmit)="onSubmit([product.name], [product.price], int)">  
  2. <div id="cartItemsList">  
  3. <ul>  
  4. <li>  
  5. <div name="product_name">{{product.name }}</div>  
  6. <div><img src="../assets/images/gallery/{{product.thumbnail}}" /></div>  
  7. <div>{{product.price }}</div>  
  8. <button class="minus-btn" (click)="minus()" type="button" name="btn">  
  9. <img src="../assets/images/minus.svg" alt="minus" /></button>  
  10. <input pattern="^(0|\+?[1-9]\d*)$" class="num" name="int" [value]="int" formControlName="int" ng-model="quantity" ng-minlength="0" type="number">  
  11. <button class="plus-btn" (click)="plus()" type="button" name="btn">  
  12. <img src="../assets/images/plus.svg" alt="plus" /></button>  
  13. <button type="submit" class="btnAddAction">Add to Cart</button>  
  14. </li>  
  15. </ul>  
  16. </div>  
  17. </form>  
in my controller:
  1. int: number=1;  
  2. i=0;  
  3.   
  4. plus(){  
  5. this.i++;  
  6. this.int=this.i;  
  7. }  
  8.   
  9.   
  10. minus(){  
  11. this.i--;  
  12. this.int=this.i;  
  13. if (this.i < 0) {  
  14. this.i = 0;  
  15. this.int=this.i;  
  16. }  
  17. }  

Answers (2)