Angular ReactiveForms 没有获得动态值
Angular ReactiveForms not getting dynamic value
我在 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>
如何在 typescript 代码中获取每个表单组的 product_total
值?
P.S。我在 HTML 代码中获取值,但在 typescript 代码中不获取值
打字稿代码是:
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);
}
我有一个类似的逻辑要实现。我在控制器中使用了这样的东西:
ngOnInit() {
this.formGroup = this.formBuilder.group({
price: ['', [Validators.required]],
quantity: ['', [Validators.required]],
total: ''
});
this.formGroup.valueChanges.subscribe(change => {
this.priceChanges()
})
}
priceChanges() {
let price = parseFloat(this.formGroup.value.price);
let quantity = parseInt(this.formGroup.value.quantity);
if (!isNaN(price) && !isNaN(quantity)) {
let totalCost = (price * quantity).toFixed(2);
this.formGroup.controls['total'].setValue(totalCost, { emitEvent: false })
}
}
处理 FormArray 的编辑版本:
请注意在您的两个输入中添加的 (ngModelChange)="detectValueChange(i)"
:
<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 (ngModelChange)="detectValueChange(i)"></td>
<td><input type="number" class="form-control" formControlName="product_quantity" value="1" #quantity (ngModelChange)="detectValueChange(i)"></td>
<td><input type="number" class="form-control" value="{{(price.value || 0) * (quantity.value || 1)}}" formControlName="product_total" disabled></td>
</tr>
</tbody>
然后,类似于我原来的解决方案,但现在在 'products' 数组
上建立索引
detectValueChange(index: number){
let controls = this.formGroup.get('products') as FormArray;
this.changeTotal(controls[index])
}
changeTotal(control) {
let price = parseFloat(control.value.product_price);
let quantity = parseInt(control.value.product_quantity);
if (!isNaN(price) && !isNaN(quantity)) {
let totalCost = (price * quantity).toFixed(2);
control['product_total'].setValue(totalCost, { emitEvent: false })
}
}
已编辑,之前的代码有{{ }},但如果我们使用[value]你不需要放{{ }},更新代码
你不需要总属于 formGroup
<input type="number" class="form-control" disabled
[value]="phone.get('product_price').value *( phone.get('product_quantity').value)" >
提交中如果您认为有必要可以制作地图
submit(form){
if (form.valid)
{
let data={
//all properties of for.value
...form.value,
//but products was products "mappeated"
products:form.value.products.map(v=>{
return {
//Which each product, return
...v, //all de properties of products +
total:v.product_price*v.product_quantity //total
}})
}
console.log(data);
}
我在 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>
如何在 typescript 代码中获取每个表单组的 product_total
值?
P.S。我在 HTML 代码中获取值,但在 typescript 代码中不获取值
打字稿代码是:
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);
}
我有一个类似的逻辑要实现。我在控制器中使用了这样的东西:
ngOnInit() {
this.formGroup = this.formBuilder.group({
price: ['', [Validators.required]],
quantity: ['', [Validators.required]],
total: ''
});
this.formGroup.valueChanges.subscribe(change => {
this.priceChanges()
})
}
priceChanges() {
let price = parseFloat(this.formGroup.value.price);
let quantity = parseInt(this.formGroup.value.quantity);
if (!isNaN(price) && !isNaN(quantity)) {
let totalCost = (price * quantity).toFixed(2);
this.formGroup.controls['total'].setValue(totalCost, { emitEvent: false })
}
}
处理 FormArray 的编辑版本:
请注意在您的两个输入中添加的 (ngModelChange)="detectValueChange(i)"
:
<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 (ngModelChange)="detectValueChange(i)"></td>
<td><input type="number" class="form-control" formControlName="product_quantity" value="1" #quantity (ngModelChange)="detectValueChange(i)"></td>
<td><input type="number" class="form-control" value="{{(price.value || 0) * (quantity.value || 1)}}" formControlName="product_total" disabled></td>
</tr>
</tbody>
然后,类似于我原来的解决方案,但现在在 'products' 数组
上建立索引detectValueChange(index: number){
let controls = this.formGroup.get('products') as FormArray;
this.changeTotal(controls[index])
}
changeTotal(control) {
let price = parseFloat(control.value.product_price);
let quantity = parseInt(control.value.product_quantity);
if (!isNaN(price) && !isNaN(quantity)) {
let totalCost = (price * quantity).toFixed(2);
control['product_total'].setValue(totalCost, { emitEvent: false })
}
}
已编辑,之前的代码有{{ }},但如果我们使用[value]你不需要放{{ }},更新代码
你不需要总属于 formGroup
<input type="number" class="form-control" disabled
[value]="phone.get('product_price').value *( phone.get('product_quantity').value)" >
提交中如果您认为有必要可以制作地图
submit(form){
if (form.valid)
{
let data={
//all properties of for.value
...form.value,
//but products was products "mappeated"
products:form.value.products.map(v=>{
return {
//Which each product, return
...v, //all de properties of products +
total:v.product_price*v.product_quantity //total
}})
}
console.log(data);
}