这种情况

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

这个问题

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


当前回答

我几乎尝试了这里所有的解决方案,但我的问题有点不同(愚蠢)。 我在路由模块中添加了组件,但没有包括它的主模块。 所以要确保你的组件是模块的一部分。

其他回答

请记住,如果你已经定义了“功能模块”,你需要在功能模块中导入,即使你已经导入到AppModule中。来自Angular文档:

模块不会继承对其他模块中声明的组件、指令或管道的访问权。AppModule导入的内容与ContactModule无关,反之亦然。在ContactComponent可以绑定[(ngModel)]之前,它的ContactModule必须导入FormsModule。

https://angular.io/docs/ts/latest/guide/ngmodule.html

import {FormsModule, ReactiveFormsModule} from '@angular/forms'; 并将其添加到app-module的imports数组中。ts文件。

好的,经过一些挖掘,我找到了一个解决方案“不能绑定到‘formGroup’,因为它不是一个已知的‘形式’属性。”

对于我的情况,我一直在使用多个模块文件,我在app.module.ts中添加了ReactiveFormsModule

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

@NgModule({
  declarations: [
    AppComponent,
  ]
  imports: [
    FormsModule,
    ReactiveFormsModule,
    AuthorModule,
],
...

但是当我从添加到另一个模块的组件中使用[formGroup]指令时,这就不起作用了,例如在author.module.ts文件中订阅的author.component.ts中使用[formGroup]:

import { NgModule }       from '@angular/core';
import { CommonModule }   from '@angular/common';
import { AuthorComponent } from './author.component';

@NgModule({
  imports: [
    CommonModule,
  ],
  declarations: [
    AuthorComponent,
  ],
  providers: [...]
})

export class AuthorModule {}

我想如果我在app.module中添加ReactiveFormsModule。默认情况下,ReactiveFormsModule将被它的所有子模块继承,比如author。模块在这种情况下…(错误的)。 我需要在author.module.ts中导入ReactiveFormsModule,以使所有指令都能工作:

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

@NgModule({
  imports: [
    ...,
    FormsModule,    //added here too
    ReactiveFormsModule //added here too
  ],
  declarations: [...],
  providers: [...]
})

export class AuthorModule {}

因此,如果您正在使用子模块,请确保在每个子模块文件中导入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();
  });
});

RC6/RC7/最终版本FIX

要修复这个错误,你只需要从模块中的@angular/forms导入ReactiveFormsModule。下面是一个导入ReactiveFormsModule的基本模块的例子:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppComponent }  from './app.component';

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        ReactiveFormsModule
    ],
    declarations: [
        AppComponent
    ],
    bootstrap: [AppComponent]
})

export class AppModule { }

为了进一步解释,formGroup是一个名为FormGroupDirective的指令的选择器,它是ReactiveFormsModule的一部分,因此需要导入它。它用于将现有的FormGroup绑定到DOM元素。你可以在Angular的官方文档页面上阅读更多信息。

RC5修复

你需要从控制器中的@angular/forms中导入{REACTIVE_FORM_DIRECTIVES},并将其添加到@Component中的指令中。这样问题就解决了。

在你修复了这个问题之后,你可能会得到另一个错误,因为你没有在表单中添加formControlName="name"到你的输入中。