这种情况

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

这个问题

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


当前回答

我没有看到这里描述的这种特定的失败机制,也没有看到任何关于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';

其他回答

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

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

对于Angular 14和使用带有路由的子模块的人

我的问题与使用带有路由的子模块有关。 要做到这一点,请遵循以下三个步骤:

1-在app.module中

imports: [
//other-modules
ReactiveFormsModule

]

2-在子模块中

imports: [
//other-modules
ReactiveFormsModule

]

3-在应用路由中。模块添加以下路由(我的模块名为AuthModule)

{
path: 'signup',
component: SignUpComponent,
children: [
  {
    path: '',
    loadChildren: () =>
      import('./features/auth/auth.module').then((m) => m.AuthModule),
  },
]

},

希望有一天这能帮助到别人!

当你在一个模态(entrycomponent)中有一个formGroup时,你也必须在模态实例化的模块中导入ReactiveFormsModule。

我在尝试做端到端测试时遇到了这个错误,没有答案让我抓狂。

如果你正在做测试,找到你的*.specs。Ts文件并添加:

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

这个问题是由于缺少FormsModule,ReactiveFormsModule的导入。我也遇到了同样的问题。 我的情况是不同的,因为我是与模块工作。所以我错过了上面的导入在我的父模块,虽然我已经导入到子模块,它不工作。

然后我导入它到我的父模块如下,它工作!

import { ReactiveFormsModule,FormsModule  } from '@angular/forms';
import { AlertModule } from 'ngx-bootstrap';

         @NgModule({
          imports: [
            CommonModule,
            FormsModule,
            ReactiveFormsModule,
    ],
      declarations: [MyComponent]
    })