【问题标题】:Error: formGroup expects a FormGroup instance. Please pass one in. - Angular 10 Error错误:formGroup 需要一个 FormGroup 实例。请传入一个。 - Angular 10 错误
【发布时间】:2021-03-23 10:40:34
【问题描述】:

我遇到了错误问题:错误:formGroup 需要一个 FormGroup 实例。请传递一个。有了这个错误,我有并行的其他错误:无法读取未定义的属性'get'。我尝试解决了这个错误,但仍然没有结果

我的代码:

component.html

<form [formGroup]="captchaForm" (submit)="submit()" class="form">

  <mat-form-field appearance="fill" class="form-group">
    <mat-label>Site Code</mat-label>
    <input
      matInput
      id="handle"
      type="text"
      formControlName="handle"
      autofocus required
    >
  </mat-form-field>

  <mat-form-field appearance="fill" class="form-group">
    <mat-label>Code from image</mat-label>
    <input
      matInput
      id="token"
      type="text"
      formControlName="token"
      autofocus required
    >
  </mat-form-field>

  <button mat-raised-button class="form-button" color="primary" type="submit">Submit</button>

</form>

component.ts

captchaForm: FormGroup;
captchaData: Captcha;
dataDisplay = false;
    
constructor(
    public formBuilder: FormBuilder,
private captchaService: CaptchaService,
){}

ngOnInit(): void {
this.getCaptcha();
this.captchaForm = new FormGroup({
      token: new FormControl(),
      handle: new FormControl(this.captchaData.handle),
    });
}

submit() {
    this.captchaService.postCaptcha(this.captchaForm.value)
      .subscribe( (response: any) => {
      this.captchaData = response;
    });
  }
public getCaptcha() {
    this.captchaService.getCaptcha().subscribe(response => {
      this.captchaData = response;
      this.dataDisplay = true;
      console.log(response);
    });
  }

model.ts

export class Captcha {
  handle: string;
  imgCaptcha: string;
  token: string;
}

captcha.service.ts

postCaptcha(token: Captcha) {
    return this.http.post(environment.ApiUrl + `/api/url`, token, {responseType: 'text'});
  }

getCaptcha() {
    return this.http.get<any>(environment.ApiUrl + `/api/urlCaptcha`);
  }

你知道如何解决这个问题吗?谢谢

【问题讨论】:

  • 你必须在你已经导入FormsModule的地方导入ReactiveFormsModule
  • 我已经在模块中导入了它们。
  • 您的代码正在使用 stackblitz(我没有包含 Material)。试着把一些formControl一个一个去掉,测试一下。

标签: angular typescript angular-forms


【解决方案1】:

您的FormGroup 在您使用它时尚未初始化。所以从ngOnInit删除它

captchaData: Captcha;
captchaForm: FormGroup = new FormGroup({
  token: new FormControl(),
  handle: new FormControl()
});

使用patchValue 更新您的表单值:

public getCaptcha() {
  this.captchaService.getCaptcha().subscribe(response => {
  this.captchaData = response;
  this.dataDisplay = true;
  console.log(response);
  this.captchaForm.patchValue({
     handle: this.captchaData.handle
    });
 });
}

【讨论】:

  • 我从ngOnInit 中删除了这段代码,并在captchaData: Captcha; 之后重新定位了这段代码,这个错误得到了解决,但现在我有另一个错误:TypeError: Cannot read property 'handle' of undefined ...这行有问题@987654329 @?
  • 从另一个 api 响应我得到 handle 数据,我需要插入这个 handle 数据来输入。
  • 点击提交后我得到响应 500,token 看起来不错,因为它正在发送数据,但 handle 发送空数据而不是从 captchaData.handle 插入数据
  • 发布代码,你从哪里获得captchaData的价值
  • 现在和提交后检查为什么你再次将值设置为captcha数据
猜你喜欢
  • 2022-12-07
  • 1970-01-01
  • 2021-01-08
  • 2018-06-11
  • 1970-01-01
  • 2017-11-25
  • 2017-09-25
  • 1970-01-01
相关资源
最近更新 更多