我的组件中有一个简单的输入,它使用[(ngModel)]:

<input type="text" [(ngModel)]="test" placeholder="foo" />

当我启动应用程序时,即使没有显示组件,也会出现以下错误。

zone.js:461未处理的Promise拒绝:模板解析错误:无法绑定到“ngModel”,因为它不是“input”的已知属性。

以下是组件。ts:

import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { Intervention } from '../../model/intervention';

@Component({
   selector: 'intervention-details',
   templateUrl: 'app/intervention/details/intervention.details.html',
   styleUrls: ['app/intervention/details/intervention.details.css']
})
    
export class InterventionDetails
{
   @Input() intervention: Intervention;
    
   public test : string = "toto";
}

当前回答

您需要导入FormsModule。

#Open app.module.ts
#add the lines

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

@NgModule({
    imports: [
       FormsModule   <--------add this 
    ],
})


if you are using reactive form then also added  ReactiveFormsModule

其他回答

从“@angular/forms”导入{FormsModule};@Ng模块({进口:[表单模块],})

如果在应用接受的解决方案后仍有人出现错误,可能是因为您有一个单独的模块文件用于要在其中使用输入标记中的ngModel属性的组件。在这种情况下,也可以在组件的module.ts文件中应用接受的解决方案。

确保[(ngModel)]实际拼写为“ngModel”,大写为M。我花了几分钟没看到这个

对我来说,问题是,我忘记在模块的声明数组中声明组件。

@NgModule({
 declarations: [yourcomponentName]
})

实际上,在大多数情况下,FormsModule已经导入。所以你要做的是确保

组件已正确添加到app.module.ts文件的声明数组中。您应该验证绑定是否拼写正确。正确拼写为[(ngModel)]