简体   繁体   English

未捕获的错误:模板解析错误:无法绑定到“ FormGroup”,因为它不是“ form”的已知属性

[英]Uncaught Error: Template parse errors: Can't bind to 'FormGroup' since it isn't a known property of 'form'

I am trying to do form binding in angular 4 . 我正在尝试在angular 4进行表单绑定。 Below is my code. 下面是我的代码。
app.component.ts app.component.ts

import { Component, Input, OnChanges, SimpleChange, OnInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  form: FormGroup;
  ngOnInit() {
      this.form = new FormGroup({
        firstname: new FormControl('First Name'),
        lastname: new FormControl(''),
        languages: new FormControl('')
      });
    }

  onSubmit = function(user) {
    console.log(user);
  };
}  

app.module.ts app.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { ProductComponent } from './product/product.component';
import { MemberComponent } from './member/member.component';
import { ItemListComponent } from './item-list/item-list.component';
import { SortPipe } from './app.sort';

@NgModule({
  declarations: [
    SortPipe,
    AppComponent,
    ProductComponent,
    MemberComponent,
    ItemListComponent
  ],
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule,
  ],
  providers: [],
  exports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule
  ],
  bootstrap: [AppComponent],
})
export class AppModule { }

app.component.html app.component.html

<form [FormGroup]="form" (ngSubmit)="onSubmit(form.value)">
    <input type="text" placeholder="firstname" name="firstname" formControlName="firstname" />
    <br>
    <input type="text" placeholder="lastname" name="lastname" formControlName="lastname" />
    <br>
    <select name="languages" formControlName="languages">
        <option value="C++">C++</option>
        <option value="Java">Java</option>
        <option value="Angular">Angular</option>
    </select>
    <br><br>
    <input type="submit" value="submit" />
</form>

But I am getting the following error 但是我收到以下错误

Uncaught Error: Template parse errors: Can't bind to 'FormGroup' since it isn't a known property of 'form'. 未捕获的错误:模板解析错误:无法绑定到“ FormGroup”,因为它不是“ form”的已知属性。 (" --> ][FormGroup]="form" (ngSubmit)="onSubmit(form.value)"> ) at JitCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.JitCompiler._compileComponents (compiler.es5.js:26882) at compiler.es5.js:26769 at Object.then (compiler.es5.js:1679) at JitCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.JitCompiler._compileModuleAndComponents (compiler.es5.js:26768) at JitCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.JitCompiler.compileModuleAsync (compiler.es5.js:26697) (“->] [FormGroup] =” form“(ngSubmit)=” onSubmit(form.value)“>),位于JitCompiler.webpackJsonp ... / .. / .. / compiler/@angular/compiler.es5。 js.JitCompiler._compileComponents(compiler.es5.js:26882)(编译器.es5.js:26769)Object.then(compiler.es5.js:1679)在JitCompiler.webpackJsonp ... / .. / .. /@angular/compiler.es5.js.JitCompiler._compileModuleAndComponents(compiler.es5.js:26768)位于JitCompiler.webpackJsonp ... / .. / .. / compiler/@angular/compiler.es5.js.JitCompiler.compileModuleAsync (compiler.es5.js:26697)

Seems you have mistyped. 您输入的内容似乎错误。

It should be f ormGroup instead of F ormGroup: 它应该是f ormGroup而不是F ormGroup:

<form [formGroup]="form"

because FormGroupDirective looks like: 因为FormGroupDirective看起来像:

@Directive({
  selector: '[formGroup]', 
  ...
})
export class FormGroupDirective extends ControlContainer implements Form,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模板解析错误:无法绑定到“ formGroup”,因为它不是“ form”的已知属性 - Template parse errors: Can't bind to 'formGroup' since it isn't a known property of 'form' 未捕获错误:模板解析错误:无法绑定到&#39;ngModel&#39;,因为它不是&#39;input&#39;的已知属性 - Uncaught Error: Template parse errors: Can't bind to 'ngModel' since it isn't a known property of 'input' 未捕获(承诺):错误:模板解析错误:由于它不是“输入”的已知属性,因此无法绑定到“上载器” - Uncaught (in promise): Error: Template parse errors: Can't bind to 'uploader' since it isn't a known property of 'input' 无法绑定到“formGroup”,因为它不是“form”的已知属性。 组件AppComponent的模板出现错误 - Can't bind to 'formGroup' since it isn't a known property of 'form'. Error occurs in the template of component AppComponent 未捕获的错误:模板解析错误:无法绑定到“配置文件”,因为它不是“ app-trnmnt-name”的已知属性 - Uncaught Error: Template parse errors: Can't bind to 'profiles' since it isn't a known property of 'app-trnmnt-name' Angular 4/5 未捕获错误:模板解析错误:无法绑定到“mat-dialog-close”,因为它不是“button”的已知属性 - Angular 4/5 Uncaught Error: Template parse errors: Can't bind to 'mat-dialog-close' since it isn't a known property of 'button' Angular2测试 - 失败:未捕获(在承诺中):错误:模板解析错误:无法绑定到“消息”,因为它不是已知的属性 - Angular2 Testing - Failed: Uncaught (in promise): Error: Template parse errors: Can't bind to 'message' since it isn't a known property of 未捕获的错误:模板解析错误:由于它不是&#39;app-quote-create&#39;的已知属性,因此无法绑定到&#39;quotes&#39; - Uncaught Error: Template parse errors: Can't bind to 'quotes' since it isn't a known property of 'app-quote-create' 无法绑定到&#39;FormGroup&#39;,因为它不是&#39;form&#39;的已知属性---- - Can't bind to 'FormGroup' since it isn't a known property of 'form' ---- 无法绑定到“ FormGroup”,因为它不是“ form”的已知属性。 (” - Can't bind to 'FormGroup' since it isn't a known property of 'form'. ("
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM