首页 > 解决方案 > 即使在添加 formsModule 和 ReactiveFormsModule 之后,ngModel 也不起作用

问题描述

app.module.ts:

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

 imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
FormsModule,
ReactiveFormsModule,
],

register.component.ts:

export class RegisterComponent implements OnInit {
public author = new Author();
constructor() {}

ngOnInit(): void {}
}

register.component.html

<input type="text" name="first" id="first" class="form-control" [(ngModel)]="author.first_name">

即使在导入所有这些之后,ngModel 仍然无法正常工作。谢谢各位帮手!!

标签: angular

解决方案


我认为您以错误的方式导入模块,正如您在 app.module.ts 文件中的代码中显示的那样。它应该像

@NgModule({
imports: [BrowserModule, FormsModule,ReactiveFormsModule],
declarations: [AppComponent],
bootstrap: [AppComponent]
})

最后,我有一个解决方案给你。我在 app.module 文件中同时导入了 FormsModule 和 ReactiveFormsModule 并使用了您的代码。它对我有用。
请检查链接:Stackblitz 解决方案链接
注意:检查 app.module.ts 和 app.component.ts 文件。希望对您有所帮助。


推荐阅读