这种情况
我试图在我的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';
对于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),
},
]
},
希望有一天这能帮助到别人!
简单的解决方案:
步骤1:导入ReactiveFormModule
import {ReactiveFormsModule} from '@angular/forms';
步骤2:将“ReactiveFormsModule”添加到导入部分
imports: [
ReactiveFormsModule
]
步骤3:重新启动应用程序并完成
例子:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {ReactiveFormsModule} from '@angular/forms';
import { EscalationManagementRoutingModule } from './escalation-management-routing.module';
import { EscalationManagementRouteWrapperComponent } from './escalation-management-route-wrapper.component';
@NgModule({
declarations: [EscalationManagementRouteWrapperComponent],
imports: [
CommonModule,
EscalationManagementRoutingModule,
ReactiveFormsModule
]
})
export class EscalationManagementModule { }
该错误表示在此模块中无法识别FormGroup。因此,您必须在每个使用FormGroup的模块中导入这些(下面)模块
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
然后将FormsModule和ReactiveFormsModule添加到模块的imports数组中。
imports: [
FormsModule,
ReactiveFormsModule
],
你可能会想,我已经在AppModule中添加了它,它应该从它继承?但事实并非如此。因为这些模块导出的是必需的指令,而这些指令只能在导入模块中使用。在共享模块中阅读更多。
导致这些错误的其他因素可能是拼写错误,如下所示…
[FormGroup]="form"大写F而不是小写F
[formsGroup]="form" form后面多出的s