FormArray中的嵌套FormGroup无法工作

时间:2019-05-29 09:52:07

标签: angular reactive-forms

我有一个带有FormArray的简单反应形式,其中将包含FromGroups。

我知道这个问题已经被问过很多次了,但是我仍然不知道为什么它不起作用。我尝试了很多方法。这是两个简化的示例,与我从文档中阅读并在网上找到的内容放在一起。

组件:

@Component({
  selector: 'app-professional-info-form',
  templateUrl: './professional-info-form.component.html',
  styleUrls: ['./professional-info-form.component.scss']
})
export class ProfessionalInfoFormComponent implements OnInit {

  protected professionalInfo: FormGroup;

  constructor() { }

  ngOnInit() {
    this.initForm();
  }

  private initForm() {
    this.professionalInfo = new FormGroup({
      trainings: new FormArray([
        new FormGroup({
          institutionId: new FormControl(null, null),
          title: new FormControl(null, null),
          description: new FormControl(null, null),
          institution: new FormControl(null, Validators.required),
          grade: new FormControl(null, null),
          from: new FormControl(null, Validators.required),
          to: new FormControl(null, null)
        })
      ])
    });

  }

}   

HTML:

<form [formGroup]="professionalInfo" (ngSubmit)="onSubmit()" novalidate *ngIf="professionalInfo">
  <div formArrayName="trainings">
    <div *ngFor="let training of trainings.controls; index as t" [formGroupName]="t">
      <input type="text" [formControlName]="title" placeholder="Titolo" />
      <input type="text" [formControlName]="institution" placeholder="Istituto" />
      <input type="text" placeholder="Inizio" [formControlName]="from">
      <input type="text" placeholder="Fine" [formControlName]="to">
      <input type="text" placeholder="Voto" [formControlName]="grade" maxlength="5">
    </div>
  </div>
</form>

控制台错误:

ERROR TypeError: Cannot read property 'controls' of undefined
    at Object.eval [as updateDirectives] (ProfessionalInfoFormComponent.html:19)

如果我将此方法添加到组件中:

get trainingsFormArray() {
    return (<FormArray>this.professionalInfo.get('trainings'));
  }

并这样编辑* ngFor:

<div *ngFor="let training of trainingsFormArray.controls; index as t" [formGroupName]="t">

控制台错误是:

ERROR Error: Cannot find control with path: 'trainings -> 0 -> '

这有点疯狂,因为如果在初始化表单后console.log'trainingsFormArray'输出如下:

console.log output

每次我必须使用angular的反应形式时,都会遇到类似这样的问题。在这种情况下,我似乎无法找出使它们与动态控件一起使用的一致方法。请帮助我。

2 个答案:

答案 0 :(得分:0)

模板中存在问题。 Angular不知道trainings是什么。使用professionalInfo.controls['trainings'].controls可以访问trainings FormArray中的控件。

类似这样的东西:

<form 
  [formGroup]="professionalInfo" 
  (ngSubmit)="onSubmit()" 
  novalidate 
  *ngIf="professionalInfo">
  <div formArrayName="trainings">
    <div 
      *ngFor="let training of professionalInfo.controls['trainings'].controls; index as t" 
      [formGroupName]="t">
      <input type="text" formControlName="title" placeholder="Titolo" />
      <input type="text" formControlName="institution" placeholder="Istituto" />
      <input type="text" placeholder="Inizio" formControlName="from">
      <input type="text" placeholder="Fine" formControlName="to">
      <input type="text" placeholder="Voto" formControlName="grade" maxlength="5">
    </div>
  </div>
</form>

  

这是您推荐的Working Sample StackBlitz

答案 1 :(得分:0)

您正在使用[formControlName]='title',这将需要title是具有控件名称的变量。您可以摆脱封闭的[],它会正常工作。用作:

formControlName='title'