这种情况

我试图在我的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 { }

这个问题

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


当前回答

如果这只是一个TypeScript错误,但是表单上的一切都正常,你可能只需要重新启动你的IDE。

其他回答

对于浏览这些线程的人来说。在我的情况下,我有一个共享模块,我只导出FormsModule和ReactiveFormsModule,忘记导入它。这导致了一个奇怪的错误,即表单组在子组件中不起作用。希望这能帮助那些摸不着头脑的人。

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

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在任何共享或应用模块。 如果创建了共享模块,则将其导入正在使用表单组和的模块中 检查该组件是否已插入 像这样声明:[ProjectComponent]。

注意:如果你在子模块的组件中工作,那么你只需要在子模块中导入ReactiveFormsModule,而不是在父应用程序根模块中。

你需要在这个模块中导入FormsModule, ReactiveFormsModule

如果你在另一个模块中使用了reactiveForm,那么你也必须执行这一步和上面的步骤:在那个特定的模块中导入reactiveFormsModule。

例如:

imports: [
  FormsModule,
  ReactiveFormsModule,
  
],