Angular Form Array,第一个索引始终为null

时间:2018-05-22 18:13:58

标签: angular angular5 angular-reactive-forms

请帮助,我试图使用角形式数组查看从组件到视图的数据。以下解决方案:Angular 5 FormArray get data from database and show rows。帮帮我,但现在我遇到一个问题,表格数组的第一个索引总是为空,我收到以下错误:

ERROR Error: Cannot find control with path: '[object Object] -> 0'
at _throwError (forms.js:2432)
at setUpFormContainer (forms.js:2405)
at FormGroupDirective.addFormGroup (forms.js:6691)
at FormGroupName.AbstractFormGroupDirective.ngOnInit (forms.js:2568) 
...

Cannot read property 'controls' of undefined
at Object.eval [as updateDirectives] 
...

在我的ngOnInit中我只有以下内容:

this.SectionForm= this.formBuilder.group({
  sectionData: this.formBuilder.array([this.buildDevelopmentSector()])
});

this.activatedRoute.params.subscribe(res =>
  // get data and push the data to a formArray called sectionDataArray
  this.GetDataFromApi(res.requestId)
);

视图包含以下部分:

  <div [formGroup] ="SectionForm">
    <div [formArrayName]="sectionDataArray">
      <div *ngFor="let section of sectionDataArray.controls ; let i = index" [formGroupName]="i">
          ...
          ...
          ...
        </div>

查看日志,我看到以下内容:

  0:
     amount:""
     percentage:""
     name:""
     quantity:""
     __proto__:Object 

  1:
     amount:1200000
     percentage:23
     name: "Repo"
     quantity:20000
     __proto__:Object

  length:2
     __proto__:Array(0)

1 个答案:

答案 0 :(得分:2)

您支持对象

sectionData

所以它是sectionDataArray,但你在这里<div [formArrayName]="sectionDataArray"> 正在考虑

sectionData

将其更改为forall