【发布时间】:2018-08-02 03:07:44
【问题描述】:
我在 Angular 中使用反应式表单,并且我有这个 FormArray,我可以从中获取除 product_total 之外的所有值。
<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" #price></td>
<td><input type="number" class="form-control" formControlName="product_quantity" value="1" #quantity></td>
<td><input type="number" class="form-control" value="{{(price.value || 0) * (quantity.value || 1)}}" formControlName="product_total" disabled></td>
</tr>
</tbody>
如何为打字稿代码中的每个表单组获取product_total 值?
附言我在 HTML 代码中获取值,但不是在打字稿代码中
打字稿代码是:
constructor(private fb: FormBuilder) { }
ngOnInit() {
const product = this.fb.group({
product_code: [],
product_name: [],
product_price: [],
product_quantity: [1],
product_total: []
});
this.myForm = this.fb.group({
customer_name: '',
customer_phone: '',
customer_address: '',
products: this.fb.array([product]),
subtotal: Number,
discount: Number,
cgst: Number,
sgst: Number,
total_price: Number,
});
}
get productForms() {
return this.myForm.get('products') as FormArray;
}
async submitForm() {
const myForm = this.myForm.value;
this.invoice = {
products: myForm.products,
};
console.log(this.invoice.products);
}
【问题讨论】:
-
如果你想要全部,你必须订阅 valueChanges,见 e.g. alligator.io/angular/reactive-forms-valuechanges。如果仅按行总计使用 {{phone.get('product_price').value *( phone.get('product_quantity').value ||1)}}
-
@Eliseo 这不是我要问的。我在表单输入中得到 product_total 值,但在打字稿代码中没有。在打字稿代码中,我得到了空值。
-
显示整行代码
-
@SurendraSinghChhabra,看看我的回答
-
@SurendraSinghChhabra 你能添加这个组件的打字稿吗?
标签: angular typescript angular-reactive-forms formarray