情况
我正在尝试在我的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 { }
问题
为什么我会收到那个错误?我有什么漏掉的吗?
xxx.module.ts
文件,问题就立即消失了。也许其他人也有同样的问题...可能是一个将来会被修复的bug。 - MikhailRatnerapp.module.ts
的declarations: [YourComponent]
中。 - Aven DestaReactiveFormsModule
,那么这个错误可能是一个误导!请确保您还将您的模块引用添加到您的应用程序模块的imports
中。 - jbobbins