微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Angular 11 无法绑定到 ngModel

如何解决Angular 11 无法绑定到 ngModel

在用 Angular 11 编写的应用程序中,我在 html 文件中有输入和 two way binding

<input type="text" class="" id="inputName"   required minlength="3" [(ngModel)]="postModel.name">

在 ts 文件我有

@Component({
  selector: 'app-home',templateUrl: './add-client.component.html',})
export class AddClientComponent implements OnInit {

  public postModel: any = {};
  public clientDTO: ClientDTO;
  public resultMessage: string;

  constructor(private route: ActivatedRoute,private router: Router,private clientService: ClientService) {
    this.postModel.name = '';
    this.resultMessage = '';

app module中:

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

imports: [
    FormsModule,ReactiveFormsModule,

绑定 two-way 不起作用。当我在 html 中输入文本时 postModel.name 仍然是空的。当我在 html 中添加 {{ postModel.name }} 并在输入中输入文本时,值不会显示

在控制台我有错误

Compiled successfully.

    Error: src/app/add-client/add-client.component.html:5:73 - error NG8002: Can't bind to 'ngModel' since it isn't a kNown property of 'input'.

    5       <input type="text" class="" id="inputName" required minlength="3" [(ngModel)]="postModel.name" style="background-color:white;">
                                                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

      src/app/add-client/add-client.component.ts:11:16
        11   templateUrl: './add-client.component.html',~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        Error occurs in the template of component AddClientComponent.

但有时没有错误但绑定不起作用。 我有Angular 11

解决方法

你说得对,声明部分的应用模块中缺少 AddClientComponent

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。