如何在另一个FormGroup内部的FormGroup内部访问FormArray?

发布于 2025-02-03 10:36:09 字数 1469 浏览 3 评论 0原文

我有一系列数据如下:

[
    {
      id: "1",
      name: "task1",
      taskForms: [
        {
          id: "8782",
          name: "ProcedureComment",
          sortOrder: null,
          description: "Comment for 'Procedure end / Suture' task",
          label: null
        },
        {
          id: "20",
          name: "form20",
          description: "form20 description",
          label: "form20 lable"
        }
      ]
    },
    {
      id: "2",
      name: "task2",
      taskForms: [
        {
          id: "87822",
          name: "ProcedureComment2",
          sortOrder: null,
          description: "Comment for 'Procedure end / Suture' task2",
          label: null
        },
        {
          id: "202",
          name: "form202",
          description: "form202 description",
          label: "form202 lable"
        }
      ]
    }
  ];

我想将taskforms转换为formarray()

我是否必须将任务标记为formgroup,然后标记 taskforms 为formarray?我该怎么做?

public formGroup = new FormGroup({
    tasks: new FormControl([]) 
//==>> i need it first as FormControl then i want to convert it and write data inside.
  });

我尝试了:

    this.formGroup.setControl('tasks', this.formBuilder.group(tasks));

但是我无法正常工作。

I have an array of data as follows:

[
    {
      id: "1",
      name: "task1",
      taskForms: [
        {
          id: "8782",
          name: "ProcedureComment",
          sortOrder: null,
          description: "Comment for 'Procedure end / Suture' task",
          label: null
        },
        {
          id: "20",
          name: "form20",
          description: "form20 description",
          label: "form20 lable"
        }
      ]
    },
    {
      id: "2",
      name: "task2",
      taskForms: [
        {
          id: "87822",
          name: "ProcedureComment2",
          sortOrder: null,
          description: "Comment for 'Procedure end / Suture' task2",
          label: null
        },
        {
          id: "202",
          name: "form202",
          description: "form202 description",
          label: "form202 lable"
        }
      ]
    }
  ];

and I want to convert taskForms to FormArray().

Do I have to mark tasks as formGroup and then mark taskForms as FormArray? How do I do this?

public formGroup = new FormGroup({
    tasks: new FormControl([]) 
//==>> i need it first as FormControl then i want to convert it and write data inside.
  });

I tried:

    this.formGroup.setControl('tasks', this.formBuilder.group(tasks));

but I couldn't make it work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ映画 2025-02-10 10:36:09

嵌套FormGroup中的FormArray

您只需使用formBuilder使您的生活更轻松:

constructor(private fb: FormBuilder) {}

ngOnInit(): void {
  this.formGroup = this.fb.group({
    nested_formgroup: this.fb.group({
      array: this.fb.array([
        { example: ['']}
      ])
    })
  })
}

结果

{
  "nested_formgroup": {
    "array": [
      {
        "example": ""
      }
    ]
  }
}

加载数据

以加载您只需将对象映射到value成员所需的数据formControls

loadData() {
  this.someService.getData().subscribe(
    data => {
      this.formGroup.get('nested_formgroup').get('array').value[0].example = data.example
    }
  )
}

这是 stackblitz for您要测试/玩。

FormArray in Nested FormGroup

You can just use the FormBuilder to make your life easier:

constructor(private fb: FormBuilder) {}

ngOnInit(): void {
  this.formGroup = this.fb.group({
    nested_formgroup: this.fb.group({
      array: this.fb.array([
        { example: ['']}
      ])
    })
  })
}

Result

{
  "nested_formgroup": {
    "array": [
      {
        "example": ""
      }
    ]
  }
}

Load the Data

To load the data you will just need to map your object to the value members of the FormControls.

loadData() {
  this.someService.getData().subscribe(
    data => {
      this.formGroup.get('nested_formgroup').get('array').value[0].example = data.example
    }
  )
}

Here's an example on Stackblitz for you to test/play with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文