Angular 具有动态添加控件的 FormArray
Angular FormArray With Dynamically Added Controls
我不久前开始在 Angular 9 上编写代码,并且正在创建一个反应式表单,其中包含通过 FormArray 摘要动态添加的表单控件,但是每当我尝试遍历值 属性 的 formArray 控件,我得到了这个令人难以置信的奇怪错误,我一直试图找出将近 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>
这是我的表格(已截断):
Please change let i = 1
instead of let i = 0
because you are using less-than equal i<=itemgroup.value.length
in for loop.
像下面这样改变你的循环条件,
for (let i = 1; i <= itemgroup.value.length; i++)
{
// your code
}
或
for (let i = 0; i < itemgroup.value.length; i++)
{
// your code
}
- 请更改方法名称
itemsgroup()
,您的方法与 formArray 同名。
在模板中将 <div *ngFor="let group of itemsgroup().controls; let i = index" class="col-xl-12">
更改为
<div *ngFor="let group of quotationForm.get('itemsgroup').controls; let i = index" class="col-xl-12">
不相关的建议
而不是在模板中的控件上使用 change
事件。您应该使用 'FormControl' 提供的 valueChanges
observable,这样您会做得更多 "Angular way".
我不久前开始在 Angular 9 上编写代码,并且正在创建一个反应式表单,其中包含通过 FormArray 摘要动态添加的表单控件,但是每当我尝试遍历值 属性 的 formArray 控件,我得到了这个令人难以置信的奇怪错误,我一直试图找出将近 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>
这是我的表格(已截断):
Please change
let i = 1
instead oflet i = 0
because you are using less-than equali<=itemgroup.value.length
in for loop.
像下面这样改变你的循环条件,
for (let i = 1; i <= itemgroup.value.length; i++)
{
// your code
}
或
for (let i = 0; i < itemgroup.value.length; i++)
{
// your code
}
- 请更改方法名称
itemsgroup()
,您的方法与 formArray 同名。 在模板中将
<div *ngFor="let group of itemsgroup().controls; let i = index" class="col-xl-12">
更改为<div *ngFor="let group of quotationForm.get('itemsgroup').controls; let i = index" class="col-xl-12">
不相关的建议
而不是在模板中的控件上使用 change
事件。您应该使用 'FormControl' 提供的 valueChanges
observable,这样您会做得更多 "Angular way".