【问题标题】:I am trying to post the data into HTTP but I am getting formGroup expects a FormGroup instance error我正在尝试将数据发布到 HTTP 中,但我得到 formGroup 需要一个 FormGroup 实例错误
【发布时间】:2018-10-17 09:33:52
【问题描述】:

CreateCollegeComponent.html:4 ERROR 错误:formGroup 需要一个 FormGroup 实例。请传一个。

例子:

<div [formGroup]="myGroup">
  <input formControlName="firstName">
</div>

在你的课堂上:

this.myGroup = new FormGroup({
   firstName: new FormControl()
});
at Function.push../node_modules/@angular/forms/fesm5/forms.js.ReactiveErrors.missingFormException (forms.js:1169)
at FormGroupDirective.push../node_modules/@angular/forms/fesm5/forms.js.FormGroupDirective._checkFormPresent (forms.js:4598)
at FormGroupDirective.push../node_modules/@angular/forms/fesm5/forms.js.FormGroupDirective.ngOnChanges (forms.js:4508)
at checkAndUpdateDirectiveInline (core.js:9239)
at checkAndUpdateNodeInline (core.js:10507)
at checkAndUpdateNode (core.js:10469)

【问题讨论】:

  • 你有没有在你的主模块中导入 FormsModule "import { FormsModule, ReactiveFormsModule } from '@angular/forms';"
  • 我已经准备好导入 FormsModule
  • 页面在myGroup 实例化之前呈现。将*ngIf="myGroup" 属性添加到div 或父元素。
  • 你可以在构造函数 this.myGroup = new FormGroup({ firstName: new FormControl() });或者你可以参考这个链接stackoverflow.com/questions/43669773/…

标签: angular


【解决方案1】:

我在 ngOnInit 中定义表单时遇到了同样的问题。将其移至构造函数即可解决问题。

【讨论】:

    猜你喜欢
    • 2017-11-25
    • 2022-01-18
    • 1970-01-01
    • 2023-04-11
    • 2021-01-08
    • 2022-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多