angular - 即使在添加 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 仍然无法正常工作。谢谢各位帮手!!
解决方案
我认为您以错误的方式导入模块,正如您在 app.module.ts 文件中的代码中显示的那样。它应该像
@NgModule({
imports: [BrowserModule, FormsModule,ReactiveFormsModule],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
最后,我有一个解决方案给你。我在 app.module 文件中同时导入了 FormsModule 和 ReactiveFormsModule 并使用了您的代码。它对我有用。
请检查链接:Stackblitz 解决方案链接
注意:检查 app.module.ts 和 app.component.ts 文件。希望对您有所帮助。
推荐阅读
- android - 带有相机插件的 awaitCameras 的 MissingPluginException
- vue.js - Storybook - Vue 组件编写为 .js 格式,而不是 .vue 格式
- firebase - 'package:flutter/src/painting/image_resolution.dart':断言失败:第 174 行 pos 15:'assetName != null':不正确
- javascript - 我想将 getElementByID 方法与 String Includes () 方法一起使用。我怎样才能做到这一点?
- api - 允许每个用户在自己的 google 日历中在 php 中创建事件
- python - 如何在日期时间模块中进行倒计时
- python - Python:决策树条件
- php - Swagger 文档中未使用 API 平台 DTO
- domain-driven-design - 一个系统可以有多个核心域吗?
- python - 如何找到用户定义范围内的所有素数?