我的组件中有一个简单的输入,它使用[(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。

这将使您的应用程序运行良好。

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {ContactListCopmponent} from './contacts/contact-list.component';
import { FormsModule }   from '@angular/forms';

import { AppComponent }  from './app.component';

@NgModule({
  imports: [
    BrowserModule,
    FormsModule
  ],
  declarations: [
    AppComponent,ContactListCopmponent
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

其他回答

您需要导入FormsModule。

打开app.module.ts并添加行

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

and

@NgModule({
    imports: [
       FormsModule
    ],
})

即使在功能模块中直接或间接导入FormsModule(例如,从共享模块),如果导入的组件未在声明中声明,也会发生此错误:

我参加了Deborah Kurata的Angular Routing课程。当我在Angular Route的组件属性上添加导入的组件ProductEditInfoComponent时,我忘记在声明属性上添加ProductEditInfoComponent。

在声明属性上添加ProductEditInfoComponent将解决NG8002:无法绑定到“ngModel”,因为它不是“input”的已知属性。问题:

我在运行Angular测试时遇到了同样的错误,因为规范文件中没有添加FormsModule。

我们需要将其添加到所有规范文件中,而为了使应用程序成功运行,我们将在app.module.ts文件中的一个位置添加它。

要解决此问题,我们必须在app.module.ts文件中包含FormModule,以避免无法绑定到ngModel,因为它不是Angular应用程序中输入问题的已知属性。

步骤如下,

打开app.module.ts并添加导入行

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

并将FormsModule添加到导入

@NgModule({
    imports: [
       FormsModule
    ],
})

图1.0(app.module.ts文件)

您需要导入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