简体   繁体   English

从 Angular 中的反应式 Forms 中的表单组中获取数组的值

[英]Get the values from array from Form Group in Reactive Forms in Angular

I have a list of form groups in a control.我在控件中有一个表单组列表。 I am trying to get array of values from the array from form groups in typescript我正在尝试从 typescript 中的表单组中的数组中获取值数组

tableGroupData : FormGroup;
CustomerForm = this.formBuilder.group(
    {
      Name: '',
      AddressTable: ''
    },
this.CustomerForm.patchValue({
     AddressTable: this.tableGroupData.controls
            });
const values = this.tableGroupData.controls.map(x => x.values) // This does not work because there is no map function for controls.

表单组数组

Controls is just an array of FormGroup. Controls 只是 FormGroup 的一个数组。 Here is an image.这是一张图片。 How can I just get the array of values from FormGroup?我怎样才能从 FormGroup 中获取值数组?

const values=[];
this.tableGroupData.controls.foreach(control=> { values.push(control.value) }

hope it will work for you希望它对你有用

If you have nested formGroups, you can instead use a formArray and then access the values of the formGroups with formArray.value .如果您有嵌套的 formGroups,则可以改为使用 formArray,然后使用 formArray.value 访问formArray.value的值。 As a result you get an array of objects with the values of each control of that formGroup.结果,您将获得一个对象数组,其中包含该 formGroup 的每个控件的值。 If you just need the values you could then map them out of each object.如果您只需要您可以使用的值,那么 map 从每个 object 中取出它们。

See this StackBlitz看到这个StackBlitz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM