Angular 11 无法绑定到 ngModel

Angular 11 Can't bind to 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