Getting Error: formGroup expects a FormGroup instance. Please pass one in

早过忘川 提交于 2019-12-06 14:37:59

formGroup expects a FormGroup instance means that you did not create an instance for the FormGroup defined in your template which is signupForm

so you have to create an instance for signupForm like this:

this.signupForm = new FormGroup({
  // form controls
  // arg1 - intial state/value of this control
  // arg2 - single validator or an array of validators
  // arg3 - async validators
  'username': new FormControl(null),
  'email': new FormControl(null),
  'gender': new FormControl('male')
});

Please double check your component code. What I noticed is that the variable you defined there is different to the one specified in your template.

You should change the reference of sigupForm to signupForm instead.

In my case, I was using Reactive Forms and loading the data for form asynchronously from a service. But, the form template will start getting generated parallelly. And at that time form would be undefined as it would be built only after the data from API call was received. So, first, check if the form is initialized, then only start generating the template.

<form class="col-sm-12 form-content" *ngIf="form" [formGroup]="form">
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!