Angular 8更改一个值,从而重置* ngfor]中的所有表单值>

问题描述 投票:0回答:1

我对每个产品价格计算都有一个Angular 8应用程序,其中包含数量,税项和价格,其中,当更改文本框中的值时,产品数量就会发生变化,并得到标签中的总和并计算出小计。

问题是:

  1. 只要更改任何输入,所有其他产品数量都会更改到输入值

  2. 如何计算所有产品的总和,使用此数量变化征税

  3. ] >>

    代码:

    <div class="product" *ngFor="let item of cart;">
      <div>{{item.price}}</div>
      <div>{{item.GST}}</div>
      <div>
        <input type="number" value="2" min="1"  (input)="inputChanged($event)">
      </div>
      <div>{{(item.price * this.cartData.quantity * item.GST)/100 + 
             (item.price*this.cartData.quantity)}}
      </div>
      </div>   
      <div class="totals">
        <div class="totals-item">
          <label>Subtotal</label>
          <div class="totals-value" id="cart-subtotal">{{this.total}}</div>
        </div>
        <div class="totals-item">
          <label>IGST</label>
        <div class="totals-value" id="cart-tax">{{this.igst}}</div>
      </div>
      <div class="totals-item">
        <label>Shipping</label>
        <div class="totals-value" id="cart-shipping">30.00</div>
      </div>
      <div class="totals-item totals-item-total">
        <label>Grand Total</label>
        <div class="totals-value" id="cart-total">{{(this.sum)+30}}</div>
      </div>
    </div> 
    

TypeScript代码:

loadData() {
    this.cartService.getCart().subscribe((data:any)=>{
        this.cart=data;
        console.log(this.cart)
    })
}

inputChanged(event) {
    console.log(event.target.value);
    this.cartData.quantity = event.target.value
    console.log(this.cart)
}

public cartTotal:any = []
public sum = 0;
public igst = 0;
public total = 0;
personalInfo:any = []

calculation() {
    this.sum = 0;
    this.igst = 0;
    this.total = 0;
    this.cartService.getCart().subscribe(data=>{
        this.cartTotal=data
        this.cartTotal.forEach(element => {
            this.sum += (((element.price*element.quantity)*18)/100) +
                          (element.price*element.quantity)
            this.igst += (((element.price*element.quantity)*18)/100)
            this.total += (element.price*element.quantity);
      });
    })
}

我对每个产品价格计算都有一个Angular 8应用程序,其中包含数量,税项和价格,其中,当更改文本框中的值时,产品数量就会发生变化,并在标签中得到总和,然后...

html angular input angular8 ngfor
1个回答
1
投票
inputChanged中的每个产品都使用相同的变量,这就是为什么如果进行更改,它也将应用于其他元素。

使用*ngFor索引,然后将值添加到数组而不是单个变量中。

© www.soinside.com 2019 - 2024. All rights reserved.