首页 > 解决方案 > FormArray 中的 Angular FormGroup 省略了特定键

问题描述

我正在使用 Angular 8 网络应用程序。

我有一个 FormGroup,其中包含一些控件和一个名为paramsFormArray 的键。该 FormArray 包含 FormGroups。

问题是我用 js 对象创建了这些 FormGroups (params) 并且它们丢失了 key name。它非常奇怪。

我将发布有用的代码:

在这里,我使用来自后端的数据创建了一个预加载的表单:

public buildPreLoadedForm() {
  this.form = this.formBuilder.group({
      ...this.currentFormula,
      params: this.formBuilder.array([]),
      formulaResult: []
  });

  this.updateFormulaParams();
}

public updateFormulaParams() {
  for (const param of this.currentFormula.params) {
    this.formParams.push(this.createFormulaParam(param));
  }
}

public createFormulaParam(formulaParam: FormulaParam) {
  return this.formBuilder.group({...formulaParam});
}

“currentFormula”中的参数是这样的:

 const param: FormulaParam = {
   ID: 0,
   CreatedAt: null,
   UpdatedAt: null,
   DeletedAt: null,
   name: 'val' + num,
   type: 'number'
 };

我在表单中推送新参数,这样做:

(this.form.get('params') as FormArray).push(this.createFormulaParam(param));

我创建的参数看起来像:

在此处输入图像描述

但是,在表单内部(在推送之后),我得到了:

刚刚创建的 param 对象

我要失去name财产了!这对我来说没有意义。

谢谢!

标签: angularangular-reactive-formsangular-formsformbuilderformarray

解决方案


推荐阅读