本文介绍了formGroup 需要一个 FormGroup 实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这里是 plunkr

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

错误:

browser_adapter.ts:82 例外:错误:未捕获(承诺):例外:./App 类应用程序中的错误 - 内联模板:1:7原始例外:formGroup 需要一个 FormGroup 实例.请传入一份.示例:<form [formGroup]="myFormGroup">原始堆栈跟踪:错误:formGroup 需要一个 FormGroup 实例.请传入一份.示例:<form [formGroup]="myFormGroup">在新的 BaseException (https://npmcdn.com/@angular/[email protected]/src/facade/exceptions.js:27:23)在 FormGroupDirective._checkFormPresent (https://npmcdn.com/@angular/[email protected]/src/directives/reactive_directives/form_group_directive.js:110:19)在 FormGroupDirective.ngOnChanges (https://npmcdn.com/@angular/[email protected]/src/directives/reactive_directives/form_group_directive.js:39:14)在 DebugAppView._View_App0.detectChangesInter
解决方案

您的代码存在一些问题

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

Plunker 示例

I have an Angular 2 RC4 basic form example on Plunkr that appears to throw the following error (In Chrome DEV console)

Here's the plunkr

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

Error:

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
解决方案

There are a few issues in your code

  • <div [formGroup]="form"> outside of a <form> tag
  • <form [formGroup]="form"> but the name of the property containing the FormGroup is loginForm therefore it should be <form [formGroup]="loginForm">
  • [formControlName]="dob" which passes the value of the property dob which doesn't exist. What you need is to pass the string dob like [formControlName]="'dob'" or simpler formControlName="dob"

Plunker example

这篇关于formGroup 需要一个 FormGroup 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 15:07