【问题标题】:How can I save my FormArray controls in Angular 2?如何在 Angular 2 中保存我的 FormArray 控件?
【发布时间】:2018-12-31 10:13:08
【问题描述】:

我用包含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>

当我想点击 post api 到服务器时,如何将整个 FormArray 控件添加到 post 方法,我的意思是如何在 FormArray 中迭代这些控件,例如将 eduFormGroup 数据保存到服务器的 saveEduFormData 方法,保存方法这些数据到服务器?

【问题讨论】:

    标签: angular formarray


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-01-21
      • 2017-12-30
      • 2018-06-23
      • 2017-09-25
      • 2019-10-10
      • 2020-09-27
      • 2018-08-29
      • 2018-06-24
      • 2017-03-19
      相关资源
      最近更新 更多