这种情况

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

这个问题

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


当前回答

我在Angular 7中也遇到了同样的问题。只需在app.module.ts文件中导入following即可。

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

然后将FormsModule和ReactiveFormsModule添加到导入数组中。

imports: [
  FormsModule,
  ReactiveFormsModule
],

其他回答

在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文件中有任何错误,它不会抛出编译时错误。

你需要在这个模块中导入FormsModule, ReactiveFormsModule以及顶层。

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

例如:

imports: [
  BrowserModule,
  FormsModule,
  ReactiveFormsModule,
  AppRoutingModule,
  HttpClientModule,
  BrowserAnimationsModule
],

对于浏览这些线程的人来说。在我的情况下,我有一个共享模块,我只导出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();
  });
});

如果正确导入了FormModule和ReactiveFormModule,可能会出现另一个问题。也许你没有将你的组件添加到app模块的模块声明中。所以把你的组件添加到app.module.ts声明中:

@NgModule({
  declarations: [
    YourNewComponent
  ]
})