如何在Angular 2中保存FormArray控件?

时间:2018-12-31 10:13:09

标签: angular formarray

我用来包含FormArray做某事。而且component.ts如下:

export class AlumniInfoComponent implements OnInit{
    eduFormGroup: FormGroup;
    socialFormGroup: FormGroup;
    alumniActsFormGroup: FormGroup;

    education: FormArray = new FormArray([]);

    constructor(private _formBuilder: FormBuilder, private 
                alumniService: AlumniInfoService) {}

    ngOnInit() {

         this.eduFormGroup = this._formBuilder.group({
             name: new FormControl('', Validators.required),
             birthPlace: new FormControl('', Validators.required),
             gender: new FormControl('', Validators.required),
             education: this.education,
          });   
      .....   
}
    addEducation() {

    (<FormArray>this.eduFormGroup.controls['education']).push(
        this._formBuilder.group({
        registrationTime: new FormControl(),
        graduationTime: new FormControl(),
        major: new FormControl('', Validators.required),
        faculty: new FormControl(),
        educationBackground: new FormControl(),
        academicDegree: new FormControl(),
      })
    );
  }

} .html代码如下:

<mat-vertical-stepper  #stepper>
     <mat-step [stepControl]="eduFormGroup">
         <form [formGroup]="eduFormGroup">
            <div>
                 <button mat-button class="circular ui icon button" 
                    (click)="addEducation()">
                     Add<mat-icon>add_circle_outline</mat-icon>
                 </button>
                  <ul class="list-group" formArrayName="education">
                     <div *ngFor="let education of 
                       eduFormGroup.controls['education'].controls; let 
                       i = index">
                          <div class="edu-class" formGroupName={{i}}>
                              <mat-form-field class="basic-info">
                                  <input
                                   matInput
                                   type="Date"
                                   formControlName="registrationTime"
                                   placeholder="registrationTime">

                              </mat-form-field>
                        </div>
                     </div>
                   </ul>
            </div>
         </form>
     </mat-step>

</mat-vertical-stepper>

当我想将api发送到服务器时,如何将整个FormArray控件添加到post方法中,这意味着如何在FormArray中迭代这些控件,例如,将eduFormGroup数据保存到服务器的saveEduFormData方法,要保存的方法这些数据发送到服务器吗?

1 个答案:

答案 0 :(得分:0)

我认为this.eduFormGroup.controls['education'].value包含了您所需的内容。