我在使用 [(ngModel)]
的组件中有这个简单的输入:
<input type="text" [(ngModel)]="test" placeholder="foo" />
当我启动我的应用程序时,我收到以下错误,即使该组件未显示。
zone.js:461 未处理的承诺拒绝:模板解析错误:无法绑定到“ngModel”,因为它不是“输入”的已知属性。
这是component.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";
}
对,就是那样。在 app.module.ts 文件中,我刚刚添加了:
import { FormsModule } from '@angular/forms';
[...]
@NgModule({
imports: [
[...]
FormsModule
],
[...]
})
为了能够对表单输入使用双向数据绑定,您需要在 Angular 模块中导入 FormsModule
包。
有关详细信息,请参阅 Angular 2 官方教程 here 和 forms 的官方文档。
import { FORM_DIRECTIVES } from '@angular/forms';
并将 FORM_DIRECTIVES
添加到指令中,是的,我的绑定再次工作(要清楚它在 rc5 发布之前工作)
FORM_DIRECTIVES
已死以防万一
用于在 Angular 2、4 & 中使用 [(ngModel)]
5+,你需要从Angular表单中导入FormsModule...
此外,它位于 GitHub 上 Angular 存储库中表单下的此路径中:
角度/包/表单/src/指令/ng_model.ts
对于 AngularJS 开发人员来说,这可能不是一件令人愉快的事情,因为您可以在任何时候使用 ng-model,但是当 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 部分 here 中的 ng_model。
您需要导入 FormsModule。
打开 app.module.ts 并添加行
import { FormsModule } from '@angular/forms';
和
@NgModule({
imports: [
FormsModule
],
})
ngModel 是 FormsModule 的一部分。它应该从 @angular/forms 导入以使用 ngModel。
请更改 app.module.ts 如下:
import { FormsModule } from '@angular/forms';
[...]
@NgModule({
imports: [
[...]
FormsModule
],
[...]
})
假设您已经创建了一个新的 NgModule,比如说 AuthModule
专门用于处理您的身份验证需求,请确保在该 AuthModule too 中导入 FormsModule
。
如果您将在 AuthModule
中使用 FormsModule
only,则您不需要将 FormModule
in 导入默认的 AppModule
。
在 AuthModule
中是这样的:
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { authRouting } from './auth.routing';
import { LoginComponent, SignupComponent } from './auth.component';
@NgModule({
imports: [
authRouting,
FormsModule
],
declarations: [
SignupComponent,
LoginComponent
]
})
export class AuthModule { }
如果您不在其他任何地方使用 FormsModule
,那么忘记在 AppModule
中导入。
您需要导入 FormsModule
。
打开 app.module.ts 并添加行
import { FormsModule } from '@angular/forms';
和
@NgModule({
imports: [
FormsModule
],
})
https://i.stack.imgur.com/rDaXp.png
简单的解决方案:在文件 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 { }
在要使用 [(ngModel)] 的那些模块中导入 FormsModule
https://i.stack.imgur.com/Ulpi6.png
要消除此错误,您需要执行两个步骤:
在您的应用程序模块中导入 FormsModule 将其作为导入值传递给 @NgModule 装饰器
基本上,文件 app.module.ts 应该如下所示:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import {AppChildComponent} from './appchild.component';
@NgModule({
imports: [ BrowserModule,FormsModule ],
declarations: [ AppComponent, AppChildComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
有时,即使我们已经导入了 BrowserModule
、FormsModule
和其他相关模块,我们仍然可能会得到相同的错误。
然后我意识到我们需要按顺序导入它们,这在我的例子中是缺失的。所以顺序应该是 BrowserModule
、FormsModule
和 ReactiveFormsModule
。
据我了解,功能模块应该遵循 Angular 的基本模块。
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule
],
providers: [],
declarations: [
AppComponent
],
bootstrap: [AppComponent]
})
export class AppModule {}
FormsModule
必须在 NgModule
之后导入。否则会出现此错误。这个答案是唯一提到订单重要性的答案!
我正在使用 Angular 7。
我必须导入 ReactiveFormsModule,因为我正在使用 FormBuilder 类来创建反应式表单。
import {
FormsModule,
ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule
], declarations: []})
如果此组件位于根模块中,则需要在根模块中导入 FormsModule,即 app.module.ts
请打开 app.module.ts
从 @angular/forms 导入 FormsModule
前任:
import { FormsModule } from '@angular/forms';
和
@NgModule({
imports: [
FormsModule
],
})
在您的应用模块中导入 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 { }
如果您需要先使用 [(ngModel)]
,则需要在 app.module.ts
中导入 FormsModule
,然后添加导入列表。像这样的东西:
文件 app.module.ts
从“@angular/forms”导入导入 {FormsModule};添加导入导入:[ BrowserModule,FormsModule ],
文件 app.component.ts
示例: 然后
我正在使用 Angular 5。
就我而言,我也需要导入 ReactiveFormsModule。
文件 app.module.ts(或 anymodule.module.ts):
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule
],
FormGroup, FormControl ,Validators
等时需要 ReactiveFormsModule
。使用 ngModel
,您不需要 ReactiveFormsModule
。
如果有人在应用已接受的解决方案后仍然出现错误,可能是因为您有一个单独的模块文件,用于要在输入标签中使用 ngModel 属性的组件。在这种情况下,也要在组件的 module.ts 文件中应用接受的解决方案。
我知道这个问题是关于 Angular 2 的,但我在 Angular 4 上,这些答案都没有帮助。
在 Angular 4 中,语法必须是
[(ngModel)]
如果您在正确导入 FormsModule 后仍然遇到错误,请检查您的终端或(Windows 控制台),因为您的项目没有编译(因为可能是另一个错误)并且您的解决方案没有反映在您的浏览器中!
就我而言,我的控制台出现以下不相关的错误:
“ApiService”类型上不存在属性“retrieveGithubUser”。
在 app.module 文件中导入 FormModule
import { FormsModule } from '@angular/forms'; [...] @NgModule({ imports: [ [...] FormsModule ], [...] })
在您愿意使用 ngModel 的模块中,您必须导入 FormsModule
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule,
],
})
export class AbcModule { }
在 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 { }
ngModel 来自 FormsModule。在某些情况下,您可能会收到此类错误:
您没有将 FormsModule 导入到声明组件的模块的导入数组中 - 使用 ngModel 的组件。您已将 FormsModule 导入到从另一个模块继承的一个模块中。在这种情况下,您有两种选择:
让 FormsModule 从两个模块导入到导入数组中:module1 和 module2。作为规则:导入模块不提供对其导入模块的访问。 (进口不继承)
将 FormsModule 声明到 module1 中的导入和导出数组中,以便在 model2 中也能看到它
(在某些版本中我遇到了这个问题)您已正确导入 FormsModule,但问题出在输入 HTML 标记上。必须为输入添加name标签属性,并且[(ngModel)]中的对象绑定名称必须与name属性中的名称相同
您需要在 app.module.ts 中导入 FormsModule 并添加行
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule
]
})
FormsModule 中声明的 ngModel 指令允许您将模板驱动表单中的控件绑定到数据模型中的属性。当您使用双向数据绑定的语法 [(ngModel)] 包含指令时,Angular 可以跟踪控件的值和用户交互,并使视图与模型保持同步。
ngModel 应该从 @angular/forms 导入,因为它是 FormsModule 的一部分。所以我建议你改变你的 app.module.ts 像这样:
import { FormsModule } from '@angular/forms';
[...]
@NgModule({
imports: [
[...]
FormsModule
],
[...]
})
对于我的场景,我必须将 [CommonModule] 和 [FormsModule] 都导入到我的模块中
import { NgModule } from '@angular/core'
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { MyComponent } from './mycomponent'
@NgModule({
imports: [
CommonModule,
FormsModule
],
declarations: [
MyComponent
]
})
export class MyModule { }
您需要导入 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
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
有时,当您尝试在其他模块中使用未共享的模块中的组件时,您会收到此错误。
例如,您有 2 个具有 module1.componentA.component.ts 和 module2.componentC.component.ts 的模块,并且您尝试在 module2 内的模板中使用来自 module1.componentA.component.ts 的选择器(例如 <module1-componentA [someInputVariableInModule1]="variableFromHTTPRequestInModule2">
),它会抛出 someInputVariableInModule1 在 module1.componentA.component.ts 中不可用的错误 - 即使您在 module1.componentA 中有 @Input() someInputVariableInModule1
。
如果发生这种情况,您希望共享 module1.componentA 以便在其他模块中访问。因此,如果您在 sharedModule 中共享 module1.componentA,module1.componentA 将可在其他模块中使用(在 module1 之外),并且导入 sharedModule 的每个模块都将能够访问其模板中的选择器,注入 @Input()
声明多变的。
这是为那些使用纯 JavaScript 而不是 Type Script 的人准备的。除了在页面顶部引用表单脚本文件,如下所示:
<script src="node_modules/@angular/forms/bundles/forms.umd.js"></script>
您还应该告诉模块加载器加载 ng.forms.FormsModule
。进行更改后,我的 NgModule
方法的 imports
属性如下所示:
imports: [ng.platformBrowser.BrowserModule, ng.forms.FormsModule],
不定期副业成功案例分享
[{ngModel}]
而不是正确的:[(ngModel)]
component.spec.ts
文件中。