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

当前回答

要在Angular 2、4和5+中使用[(ngModel)],需要从Angular表单导入FormsModule。。。

此外,它位于GitHub上Angular存储库中表单下的路径中:

angular/packages/forms/src/directives/ng_model.ts

对于AngularJS开发人员来说,这可能不是一件很愉快的事情,因为您可以在任何时候在任何地方使用ng模型,但是当Angular尝试分离模块以使用您当时想要使用的任何东西时,ngModel现在在FormsModule中。

此外,如果您使用的是ReactiveFormsModule,则也需要导入它。

因此,只需查找app.module.ts并确保导入了FormsModule。。。

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';  // <<<< import it here
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule, FormsModule // <<<< And here
  ],
  providers: [],
  bootstrap: [AppComponent]
})

export class AppModule { }

此外,以下是FormsModule中Angular4 ngModel的当前开始注释:

/**
 * `ngModel` forces an additional change detection run when its inputs change:
 * E.g.:
 * ```
 * <div>{{myModel.valid}}</div>
 * <input [(ngModel)]="myValue" #myModel="ngModel">
 * ```
 * I.e. `ngModel` can export itself on the element and then be used in the template.
 * Normally, this would result in expressions before the `input` that use the exported directive
 * to have and old value as they have been
 * dirty checked before. As this is a very common case for `ngModel`, we added this second change
 * detection run.
 *
 * Notes:
 * - this is just one extra run no matter how many `ngModel` have been changed.
 * - this is a general problem when using `exportAs` for directives!
 */

如果您想使用输入,而不是表单,可以将其与ngModelOptions一起使用,并使其独立为true。。。

[ngModelOptions]="{standalone: true}"

有关更多信息,请查看此处Angular部分中的ng_model。

其他回答

在app.module文件中导入FormModule

import { FormsModule } from '@angular/forms'; [...] @NgModule({ imports: [ [...] FormsModule ], [...] })

ngModel来自FormsModule。在某些情况下,您可能会收到此类错误:

您没有将FormsModule导入到声明组件的模块导入数组中,即使用ngModel的组件。您已经将FormsModule导入一个模块,该模块继承了另一个模块。在这种情况下,您有两个选项:

让FormsModule从两个模块(module1和module2)导入导入数组。通常:导入模块不会提供对其导入模块的访问权限。(导入不继承)将FormsModule声明到module1中的导入和导出数组中,以便能够在model2中看到它

(在某些版本中,我遇到了这个问题)您正确导入了FormsModule,但问题出在输入HTML标记上。必须为输入添加name标记属性,[(ngModel)]中的对象绑定名称必须与name属性中的名称相同

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

简单解决方案:在文件app.module.ts中-

示例1

import {FormsModule} from "@angular/forms";
// Add in imports

imports: [
 BrowserModule,
 FormsModule
 ],

示例2

如果要使用[(ngModel)],则必须在app.module.ts中导入FormsModule:

import { FormsModule  } from "@angular/forms";
@NgModule({
  declarations: [
    AppComponent, videoComponent, tagDirective,
  ],
  imports: [
    BrowserModule,  FormsModule

  ],
  providers: [ApiServices],
  bootstrap: [AppComponent]
})
export class AppModule { }

在ngModule中,您需要导入FormsModule,因为ngModel来自FormsModule。请按照我分享的以下代码修改app.module.ts

import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

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