首页 > 解决方案 > 无法修补空白表单数组

问题描述

我有一个表格:

this.myForm = this.fBuilder.group({
            speeches: this.fBuilder.array([])
});

然后稍后我想用来自另一个组件的外部阵列来修补整个演讲阵列。示例数据:

newData = [{id: "wavesurfer_mt87bwp5cyi", start: 0.8766667991737204, duration: 2.006666969971786, characterId: 0},
{id: "wavesurfer_glxyt1be2js", start: 4.0033339384320845, duration: 2.2600003415961307, characterId: 1}]

我想像这样修补它:

this.myForm.controls.speeches.patchValue(newData);

但是 Angular 没有将空白数组修改为我想要的对象数组,怎么会?我必须始终将每个属性作为适当的表单控件吗?我不需要它,因为这些值永远不会在实际的表单字段中使用。

标签: angular

解决方案


如果您只是想针对 formControl“演讲”存储值数组,则不需要为此使用 FormArray。

this.myForm = this.fBuilder.group({
     speeches: [[]]
});

this.myForm.controls.speeches.patchValue(newData);

推荐阅读