我的组件中有一个简单的输入,它使用[(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/forms”导入{FormsModule};@Ng模块({进口:[表单模块],})

其他回答

当我第一次做这个教程时,main.ts看起来与现在略有不同。它看起来非常相似,但请注意其中的差异(上面的一个是正确的)。

对的:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

platformBrowserDynamic().bootstrapModule(AppModule);

旧教程代码:

import { bootstrap }    from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
bootstrap(AppComponent);

要在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。

确保采用模板驱动的表单方法(https://angular.io/guide/forms),正如@Nikita Sychou已经提到的,您已经用“name”属性修饰了输入字段,例如

<form>
   <input [(ngModel)]="form.email" name="email"></input>

并且FormsModule已导入关联的@NgModule中。这两者都是避免“无法绑定到ngModel”错误的前提条件。如果IDE中仍然存在样式错误,例如IntelliJ抱怨ngModel指令,请忽略它们。。你很好去:-)

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

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

如果在正确导入FormsModule后仍然出现错误,请检查您的终端或(windows控制台),因为您的项目未在编译(因为可能是其他错误),并且您的解决方案尚未反映在浏览器中!

在我的情况下,我的控制台出现了以下不相关的错误:

类型“ApiService”上不存在属性“retrieveGithubUser”。