首页 > 解决方案 > 使用 Angular 的动态响应式表单

问题描述

我创建了一个动态反应表单,除了第一项FormArray显示为空外,它工作正常。

电流输出 电流输出

预期产出 预期产出

数组的后续项目生成良好。如果我在生成其他项目后删除第一个项目,则数据将正确保存。

我在控制台上看到以下错误并尝试寻找解决方案,但根据网络上的各种答案,我已经正确完成了。

ERROR Error: Cannot find control with path: 'contentBoxes -> 0 -> contentType'

Unable to display error. Open your browser's console to view.

Angular is running in the development mode. Call enableProdMode() to enable the production mode.

ERROR Error: Cannot find control with path: 'contentBoxes -> 0 -> revenueShare'

Unable to display error. Open your browser's console to view.

代码可以在这里查看和运行:https ://stackblitz.com/edit/angular-ui5n8c

非常感谢您的帮助。

标签: angular

解决方案


您将函数的引用放在索引 0: this.buildGroup; 那当然应该是:this.buildGroup()。这样,将调用函数并返回实际对象。所以你的问题是,该功能没有被执行。

所以总结一下:

```

  buildArray(): FormArray {
    this.contentBoxes = this._fb.array([
      this.buildGroup() // <---- notice the () I added, these are needed!
      //this._fb.control('')
    ]);
    return this.contentBoxes;
  }

```


推荐阅读