这种情况
我试图在我的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 { }
这个问题
为什么会出现这个错误?我遗漏了什么吗?
如果你必须导入两个模块,然后像下面这样添加
import {ReactiveFormsModule,FormsModule} from '@angular/forms';
@NgModule({
declarations: [
AppComponent,
HomeComponentComponent,
BlogComponentComponent,
ContactComponentComponent,
HeaderComponentComponent,
FooterComponentComponent,
RegisterComponent,
LoginComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
routes,
ReactiveFormsModule
],
providers: [],
bootstrap: [AppComponent]
})
对于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),
},
]
},
希望有一天这能帮助到别人!
我也有同样的问题。确保如果使用子模块(例如,你不仅有app.component.module.ts),而且有一个单独的组件,如login.module。在这个login.module.ts导入中包含ReactiveFormsModule导入,这样它才能工作。我甚至不需要在我的app.component.module中导入ReactiveFormsModule,因为我使用了所有的子模块。
文件login.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { IonicModule } from '@ionic/angular';
import { LoginPageRoutingModule } from './login-routing.module';
import { LoginPage } from './login.page';
@NgModule({
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule,
IonicModule,
LoginPageRoutingModule
],
declarations: [LoginPage]
})
export class LoginPageModule {}
好的,经过一些挖掘,我找到了一个解决方案“不能绑定到‘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。