angular - Angular 表单数组,第一个索引始终为空
问题描述
请协助,我正在尝试使用角度表单数组查看从组件到视图的数据。以下解决方案:Angular 5 FormArray 从数据库中获取数据并显示行。帮助了我,但现在我遇到了一个问题,表单数组的第一个索引始终为空,并且我收到以下错误:
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)
解决方案
你的支持对象有
sectionData: this.formBuilder.array([this.buildDevelopmentSector()])
所以它sectionData
,但你指的是sectionDataArray
这里
<div [formArrayName]="sectionDataArray">
将其更改为sectionData
推荐阅读
- typescript - Typescript - 一些省略实现从属性中删除可选性
- algorithm - 将冒泡排序更改为插入排序,对迭代进行少量更改
- python - 遍历 Jinja2 模板中的字典列表
- javascript - 服务器响应显示在页面中,无法在客户端处理
- blender - Blender 2.79 材料导出到 gltf 失去光泽
- flutter - Flutter中的主题覆盖不起作用,使用官方示例
- vue.js - 如何将抽屉图标和标题移动到工具栏的左侧
- jsf - 如何正确使用@NotNull 注解?
- visual-studio - 面向 .NET Core 2.2 的 Azure 函数
- powershell - 如果 *.tmp 存在于其中的任何位置,如何不对目录进行操作?