formGroup需要一个FormGroup实例

问题描述 投票:33回答:3

我在Plunkr上有一个Angular 2 RC4基本表单示例,似乎抛出以下错误(在Chrome DEV控制台中)

这是掠夺者

https://plnkr.co/edit/GtPDxw?p=preview

错误:

browser_adapter.ts:82 EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in ./App class App - inline template:1:7
ORIGINAL EXCEPTION: formGroup expects a FormGroup instance. Please pass one in.
           Example: <form [formGroup]="myFormGroup">

ORIGINAL STACKTRACE:
Error: formGroup expects a FormGroup instance. Please pass one in.
           Example: <form [formGroup]="myFormGroup">

    at new BaseException (https://npmcdn.com/@angular/[email protected]/src/facade/exceptions.js:27:23)
    at FormGroupDirective._checkFormPresent (https://npmcdn.com/@angular/[email protected]/src/directives/reactive_directives/form_group_directive.js:110:19)
    at FormGroupDirective.ngOnChanges (https://npmcdn.com/@angular/[email protected]/src/directives/reactive_directives/form_group_directive.js:39:14)
    at DebugAppView._View_App0.detectChangesInter
angular angular2-forms angular2-formbuilder
3个回答
51
投票

您的代码中存在一些问题

  • <div [formGroup]="form">标签之外的<form>
  • <form [formGroup]="form">,但包含FormGroup的属性的名称是loginForm因此它应该是<form [formGroup]="loginForm">
  • [formControlName]="dob"传递了不存在的财产dob的价值。你需要的是传递字符串dob[formControlName]="'dob'"或更简单的formControlName="dob"

Plunker example


2
投票

我正在使用反应形式并遇到类似的问题。帮助我的是确保我在课堂上设置了相应的FormGroup。像这样的东西:

myFormGroup: FormGroup = this.builder.group({
    dob: ['', Validators.required]
});

1
投票

当我指定fromGroupName而不是formArrayName时,我遇到了这个错误。

确保正确指定它是表单数组还是表单组。

<div formGroupName="formInfo"/>

<div formArrayName="formInfo"/>

© www.soinside.com 2019 - 2024. All rights reserved.