在Angular中以反应形式更改值

伊苏伦德拉辛格

我有一个包含表格的循环表行。我想根据反应式形式的价格和数量更新总字段。代码是

<tbody formArrayName="products">
    <tr *ngFor="let phone of productForms.controls; let i=index" [formGroupName]="i">
      <th scope="row">{{i+1}}</th>
      <td><input type="text" class="form-control" placeholder="Product Code" formControlName="product_code"></td>
      <td><input type="text" class="form-control" placeholder="Product Name" formControlName="product_name"></td>
      <td><input type="number" class="form-control" placeholder="Price" formControlName="product_price"></td>
      <td><input type="number" class="form-control" placeholder="Quantity" formControlName="product_quantity"></td>
      <td><input type="number" class="form-control"  formControlName="total" disabled></td>
      <td><button type="button" class="btn btn-danger ml-2" (click)="deleteProduct(i)">Remove</button></td>
    </tr>
  </tbody>

输入价格和数量时如何更新总值。打字稿代码为:

ngOnInit() {
this.myForm = this.fb.group({
  customer_name: '',
  customer_phone: '',
  customer_address: '',
  products: this.fb.array([0])
});
}
get productForms() {
return this.myForm.get('products') as FormArray;
}
addProduct() {
    const product = this.fb.group({
      product_code: [],
      product_name: [],
      product_price: [],
      product_quantity: [],
      total: []
    });

    this.productForms.push(product);
  }
马尔萨尔马维

您可以考虑使用这样的模板变量

<td><input type="number" class="form-control" placeholder="Price" formControlName="product_price" #price></td>
<td><input type="number" class="form-control" placeholder="Quantity" formControlName="product_quantity" #quantity></td>
<td><input type="number" class="form-control"  [value]="(price.value || 0) * (quantity.value || 0)" disabled></td>

得到所有产品的总数

零件

getProductsTotalPrice() : number { 
   let total = 0;
   for(let product of  productForms.controls){
     total += (+product.get('price').value || 0) * (+product.get('quantity').value || 0)
   }
    return total;
  }

模板

 <span>{{getProductsTotalPrice}}</span>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何获取以Angular反应形式更改了值的字段的FormControlName

Angular反应形式:如何获取刚刚更改的值

以反应形式Angular 4在行中显示和输入值

Angular反应形式中的Null值嵌套FormGroup

如何在Angular中获取反应形式输入的值?

Primeng Autocomplete - 不使用 Angular 中的反应形式修补值

Angular 4反应形式-显示值

Angular getRawValue()不从反应形式获取[值]

在 Angular 中以 Angular 反应形式创建表

为什么在Jasmine Angular单元测试中未触发反应形式控件的更改事件?

在 Angular 5 中,在特定索引处的反应形式数组中插入值

在 Angular 反应形式中嵌套在 FormArray 的 FormArray 中的 FormArray 的补丁值

反应形式:使用 angular 4 注入形式值服务

获取反应形式中的值

在Angular的反应形式中输入Type =“ Range”

如何从Angular反应形式中删除字段?

密码必须与Angular中的反应形式匹配

Angular中的动态列反应形式

反应形式-在Angular中追加值

在Angular中以动态反应形式添加项目

如何清除Angular反应形式中的FormArray

Angular 中反应形式的密码匹配验证

子组件中的Angular反应形式提交

如何使用 Angular 中的反应形式获取选择选项下拉文本值

如何在angular2中以反应形式存储多个复选框值?

检查反应形式值是否从初始值更改

简单的Angular2反应形式控件启用状态更改

Angular 2反应形式-检测组件上的输入更改事件

Angular 2反应形式:更新值更改选择列表