这种情况

我试图在我的Angular应用程序中创建一个非常简单的表单,但无论如何,它都无法工作。

Angular版本

Angular 2.0.0 RC5

这个错误

不能绑定到'formGroup'因为它不是'form'的已知属性

的代码

视图

<form [formGroup]="newTaskForm" (submit)="createNewTask()">
   <div class="form-group">
      <label for="name">Name</label>
      <input type="text" name="name" required>
   </div>
   <button type="submit" class="btn btn-default">Submit</button>
</form>

控制器

import { Component } from '@angular/core';
import { FormGroup, FormControl, Validators, FormBuilder }  from '@angular/forms';
import {FormsModule,ReactiveFormsModule} from '@angular/forms';
import { Task } from './task';

@Component({
    selector: 'task-add',
    templateUrl: 'app/task-add.component.html'

})
export class TaskAddComponent {

    newTaskForm: FormGroup;

    constructor(fb: FormBuilder)
    {
        this.newTaskForm = fb.group({
            name: ["", Validators.required]
        });
    }

    createNewTask()
    {
        console.log(this.newTaskForm.value)
    }
}

《ngModule

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule }   from '@angular/forms';

import { routing }        from './app.routing';
import { AppComponent }  from './app.component';
import { TaskService } from './task.service'

@NgModule({
    imports: [
        BrowserModule,
        routing,
        FormsModule
    ],
    declarations: [ AppComponent ],
    providers: [
        TaskService
    ],
    bootstrap: [ AppComponent ]
})
export class AppModule { }

这个问题

为什么会出现这个错误?我遗漏了什么吗?


当前回答

在相应的模块中导入ReactiveFormsModule。

其他回答

在过去的3天里,我一直在与这个错误作斗争。我添加了ReactiveFormsModule和FormsModule,如上所述,在我的两个模块的评论中,但它没有任何效果,直到我用另一个“ng serve”重新加载我的项目。 我不知道为什么它没有自动重载,但至少我很高兴它终于工作了! 请解释一下好吗?

转到:app.module.ts 进口:[ … ReactiveFormsModule ] import {ReactiveFormsModule} from '@angular/forms'; 它应该是这样的:

从'@angular/forms'中导入{ReactiveFormsModule}; @NgModule ({ 声明:[ ], 进口:[ ReactiveFormsModule ], 供应商:[], 引导(AppComponent): }) 导出类AppModule {}

如果你在开发一个组件时遇到了这个问题,你应该把这两个模块添加到你最近的模块中:

import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
   // other modules
    FormsModule,
    ReactiveFormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

如果你正在为你的组件开发一个测试,那么你应该像这样将这个模块添加到你的测试文件中:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ContactusComponent } from './contactus.component';
import { ReactiveFormsModule } from '@angular/forms';

describe('ContactusComponent', () => {
  let component: ContactusComponent;
  let fixture: ComponentFixture<ContactusComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ContactusComponent],
      imports:[
        ReactiveFormsModule
      ]

    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(ContactusComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

ReactiveFormsModule和FormsModule导入应该添加到您的自定义组件模块中,以及调用它的父组件中。

例如,我需要为过滤器组件添加表单。所以我应该在我的过滤器模块和它的父页面(可能是列表)模块中添加这个导入,从这个过滤器按钮被单击的地方开始。

在app.module.ts中导入并注册ReactiveFormsModule。

import { ReactiveFormsModule } from '@angular/forms';

@NgModule({
declarations: [
AppComponent,
HighlightDirective,
TestPipeComponent,
ExpoentialStrengthPipe

],
imports: [
BrowserModule,
ReactiveFormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

请确保.ts和.html文件中的拼写正确。 xxx.ts

  profileForm = new FormGroup({
  firstName: new FormControl(''),
 lastName: new FormControl('')
 });

xxx.html文件-

  <form [formGroup]="profileForm"> 
  <label>
  First Name:
   <input type="text" formControlName = "firstName">
  </label>

  <label>
  Last Name:
   <input type="text" formControlName = "lastName">
   </label>
   </form>

我错误地写了[FormGroup]而不是[FormGroup]。在.html中检查拼写是否正确。如果.html文件中有任何错误,它不会抛出编译时错误。