我无法从Angular6的FormArray获取值

时间:2018-08-29 10:53:22

标签: javascript angular

我无法获取表单数组数据。在这里,我在一个表单组和一个表单数组中使用两个字段。现在,我想从表单组和表单数组中获取所有数据。 建议解决方案以消除此错误

这是我的.ts代码

', '.join(set(filter(None, [i.strip() for i in string.split(',')])))

HTML代码:

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators, FormArray } from 
'@angular/forms';
@Component({
  selector: 'app-basic-forms',
  templateUrl: './basic-forms.component.html',
  styleUrls: ['./basic-forms.component.css']
})
export class BasicFormsComponent implements OnInit {
  registerForm: FormGroup;
  constructor(private formBuilder: FormBuilder) { }
  ngOnInit() {
    this.registerForm = this.formBuilder.group({
      firstName: ['', Validators.required],
      lastName: ['', Validators.required],
      aliases: this.formBuilder.array([])
    });
  }
  get aliases() {
    return this.registerForm.get('aliases') as FormArray;
  }
  addAlias() {
    this.aliases.push(this.formBuilder.group({
      Name: [''],
      Age: ['']
    }));
  }
  onSubmit() {
    alert(Success!!\n\n ${JSON.stringify(this.registerForm.value)});
  }
}

Output

1 个答案:

答案 0 :(得分:2)

只需将formGroupName="i"更改为[formGroupName]="i"