这种情况

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

这个问题

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


当前回答

即使您已经导入了FormsModule和ReactiveFormsModule,也会收到此错误消息。我将一个组件(使用[formGroup]指令)从一个项目移动到另一个项目,但未能将该组件添加到新模块中的declarations数组中。这导致不能绑定到'formGroup',因为它不是'form'错误消息的已知属性。

其他回答

我没有看到这里描述的这种特定的失败机制,也没有看到任何关于public-api的引用。在这个问题的其他地方,所以添加它,以防它帮助其他人。

我在一个特性模块中使用响应式表单构建了一个组件。按照其他回答中描述的指导,我的模块构建没有问题,直到我试图通过public-api.ts导出组件。在这一点上,我开始得到熟悉的错误不能绑定到'formGroup',因为它不是'形式'的已知属性,没有其他迹象可能是错误的。

这个问题发生在我身上,因为我忘记从NgModule中声明和导出这个组件。一旦我把它添加到那里,模块就开始重新构建。因此,在NgModule中尝试导出响应式表单组件时,要记住三件事:

import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { MyExportedComponent } from './components/my-exported.component';


@NgModule({
    declarations: [
        MyExportedComponent // this
  ],
    imports: [
        ReactiveFormsModule // this
    ], 
    exports: [
        MyExportedComponent // and this
    ],
    providers: [
        ]
})
export class MyModule { }

此时,Angular会在public-api.ts中使用这行代码:

export * from './lib/my-module/components/my-exported.component';

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

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

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

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

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

我的解决方案很微妙,我没有看到它已经列出。

我在Angular Materials Dialog组件中使用了响应式表单,该组件没有在app.module.ts中声明。主组件在app.module.ts中声明,并将打开对话框组件,但在app.module.ts中没有显式声明对话框组件。

我正常使用对话框组件没有任何问题,除了每当我打开对话框时,表单抛出这个错误:

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

如果,已经导入

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

在rootModule和customComponentModule中仍然得到错误

NG8002:不能绑定到'formGroup',因为它不是已知的属性 “形式”

只是,

重新启动应用服务器

这将解决问题