不久前我开始在 Angular 9 上进行编码,并且正在创建一个反应式表单,它具有通过 FormArray Abstract 动态添加的表单控件,但是每当我尝试循环遍历 formArray 控件的 value 属性时,我就会感到难以置信错误我已经尝试了将近 12 个小时。当我在值节点下只有一个数组条目时,不会出现此错误。有人可以指出做错了什么吗?
这是错误:
这是我的表单生成器:
this.quotationForm = this.fb.group({
customerId : new FormControl('', Validators.required),
customerName : new FormControl(),
'contact' : this.fb.group({
customerAddress : new FormControl('',Validators.required),
customerPhone : new FormControl('', Validators.required),
customerEmail : new FormControl('', Validators.email),
}),
'itemsgroup' : new FormArray(
[
this.fb.group({
item : '',
quantity : '',
amount : ''
})
],{validators : Validators.required}),
currency : new FormControl(),
discount : new FormControl(),
taxRate : new FormControl(),
taxAmount : new FormControl(),
purchase : new FormControl(),
note : new FormControl(),
total :new FormControl(),
ref : new FormControl(),
tailor : this.usrId,
datecreated : new FormControl(this.model, Validators.required),
});
这是我的组件的函数调用:
getSelectedItemsSum(){
let quotesTotal = 0;
let itemsgroupAmt = 0;
let itemsgroupQty = 0;
let itemsgroup = this.itemsgroup();
if(itemsgroup.touched == true && itemsgroup.status !== 'INVALID'){
if(itemsgroup.value.length > 1){
console.log(itemsgroup.value);
for(let i = 0; i <= itemsgroup.value.length; i++){
if((itemsgroup.value[i].amount != '' || itemsgroup.value[i].amount != 0) && (itemsgroup.value[i].quantity != '' || itemsgroup.value[i].quantity != '')){
itemsgroupAmt += itemsgroup.value[i].amount;
itemsgroupQty += itemsgroup.value[i].quantity;
}
}
quotesTotal = (itemsgroupAmt * itemsgroupQty);
}else{
if((itemsgroup.value[0].amount != '' || itemsgroup.value[0].amount != 0) && (itemsgroup.value[0].quantity != '' || itemsgroup.value[0].quantity != 0))
quotesTotal = (parseInt(itemsgroup.value[0].amount) * parseInt(itemsgroup.value[0].quantity));
}
}else return;
console.log(itemsgroup)
}
这是itemsgroup() function
itemsgroup() : FormArray{
return this.quotationForm.get('itemsgroup') as FormArray;
}
我的 html 模板的代码片段:
<div formArrayName="itemsgroup" class="row row-xs mb-2">
<div *ngFor="let group of itemsgroup().controls; let i = index" class="col-xl-12">
<div [formGroupName]="i" class="row row-xs mb-2 relative">
<div class="col-md-4">
<input type="text" formControlName="item" placeholder="Item {{i + 1}}" class="form-control" required>
</div>
<div class="col-md-4">
<input
type="number"
formControlName="quantity"
placeholder="Quantity {{ i + 1 }}"
min="1" step="1" value="1"
class="form-control" required>
</div>
<div class="col-md-4">
<input
(change)="getSelectedItemsSum()"
type="number"
formControlName="amount"
placeholder="Amount {{ i + 1 }}"
class="form-control" required>
</div>
<button
(click)="removeItemGroup(i)"
*ngIf="(i >= 1)"
mat-fab
mat-stroked-button
color="primary"
matTooltip="Delete Item Group"
style="position:absolute;right:7px;top:17px;width:22px;height:22px;">
<i style="position:absolute;top:5px;left:8px">*</i>
</button>
</div>
</div>
</div>