首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >formGroups数组的角-formGroups()设置整个formArray值的最后一个对象

formGroups数组的角-formGroups()设置整个formArray值的最后一个对象
EN

Stack Overflow用户
提问于 2019-10-18 20:33:16
回答 1查看 116关注 0票数 0

我有这样一个表单,它包含一个表单组objform数组。

代码语言:javascript
复制
this.objform = this.fb.group({
  val: null,
  string: '',
  arr: this.fb.array([null, null, null])
})
this.nestedform = this.fb.group({
  arr: this.fb.array([this.objform, this.objform, this.objform]),
  test1: null,
  test2: ""
})

我遇到了一个问题,当我setValue() of this.nestedform时,arr的值是数组中的最后一个obj。为什么会发生这种情况?我只是犯了个愚蠢的错误吗?

谢谢

如果你想看完整的例子,我有一个stackblitz 这里

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-19 10:35:03

您要将相同的对象分配给nestedForm中的数组。使用返回formGroup的函数

代码语言:javascript
复制
  getGroup()
  {
    return this.fb.group({
      val: null,
      str: '',
      arr: this.fb.array([null, null, null])
    })
  }

并在创建窗体时使用

代码语言:javascript
复制
this.nestedform = this.fb.group({
  arr: this.fb.array([this.getGroup(), this.getGroup(), this.getGroup()]),
  test1: null,
  test2: ""
})

所以,您有一个不同的“对象”

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58457822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档