javascript - 具有动态添加控件的 Angular FormArray

标签 javascript angular multidimensional-array angular-reactive-forms angular-abstract-control

我不久前开始在 Angular 9 上编码,并创建一个响应式(Reactive)表单,其中包含通过 FormArray Abstract 动态添加的表单控件,但每当我尝试循环 formArray 控件的 value 属性时,我得到这个令人难以置信的奇怪错误我花了近 12 个小时试图找出答案。当我在值节点下只有一个数组条目时,不会出现此错误。有人可以指出我做错了什么吗?

这是错误:

enter image description here

这是我的表单生成器:

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()函数

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>

这是我的表格(已 chop ): enter image description here

最佳答案

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

}

关于javascript - 具有动态添加控件的 Angular FormArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60665244/

相关文章:

javascript - Aurelia View 中的脚本标签未执行

javascript - 类型错误 : Failed to execute 'createObjectURL' on 'URL' : Overload resolution failed

angular - yarn 错误未知工作区 - 为什么 `yarn workspace` 会因 `nohoist` ed 包而失败?

php - 构建多维然后添加到Table中

arrays - Delphi中N数组的交集

javascript - 如何在 Node.js Express 中让我的 session 最后跨子域?

javascript - 如何在 Node.js 上的 Controller 或外部文件上使用 Web 套接字?我无法访问 io 对象

Angular 7 HttpClient 将请求参数发送到端点的空值

go - 无法使用 dart 和 angular 2 客户端运行 Golang 后端

java - Java 中的 int[large][small] 或 int[small][large] 之间是否存在低级差异?