我正在使用 Angular 4,但在控制台中出现错误:
无法绑定到“ngModel”,因为它不是“输入”的已知属性
我该如何解决这个问题?
我正在使用 Angular 4,但在控制台中出现错误:
无法绑定到“ngModel”,因为它不是“输入”的已知属性
我该如何解决这个问题?
为了对表单输入使用双向数据绑定,您需要FormsModule
在 Angular 模块中导入包。
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule
]
编辑
由于有很多相同问题的重复问题,我正在加强这个答案。
有两个可能的原因
缺少FormsModule
,因此将其添加到您的模块中,
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule
]
[(ngModel)]
检查输入标签中的语法/拼写
这是一个正确的答案。你需要导入'FormsModule'
首先在 app.module.ts
**
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
FormsModule,
ReactiveFormsModule ,
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
** app.component.spec.ts 中的第二个
import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
FormsModule
],
declarations: [
AppComponent
],
}).compileComponents();
}));
最好的问候和希望会有所帮助。
你ngModel
没有工作,因为它还不是你的一部分NgModule
。
您必须告诉NgModule
您有权在ngModel
整个应用程序中使用,您可以通过添加FormsModule
到您的app.module.ts
-> imports
->中来做到这一点[]
。
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [ FormsModule ], // HERE
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
上述问题的所有解决方案都是正确的。但是如果您使用延迟加载,则FormsModule
需要在其中包含表单的子模块中导入。添加它app.module.ts
不会有帮助。
我在使用 Karma/Jasmine 测试我的 Angular 6 应用程序时遇到了这个错误。我已经FormsModule
在我的顶级模块中导入了。但是当我添加一个使用我的测试的新组件时,[(ngModel)]
我的测试开始失败。在这种情况下,我需要FormsModule
在我的TestBed
TestingModule
.
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
FormsModule
],
declarations: [
RegisterComponent
]
})
.compileComponents();
}));
在app.module.ts
添加这个:
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
declarations: [AppComponent],
imports: [FormsModule],
})
对我来说答案是错误的拼写ngModel
。我把它写成这样:ngModule
虽然它应该是ngModel
。
所有其他尝试显然都无法为我解决错误。
在这个问题上花了几个小时后在这里找到了解决方案
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule,
ReactiveFormsModule
]
})
尝试添加
模块级别的 ngModel
代码和上面一样
用Angular 7.xx更新,在我的一个模块中遇到同样的问题。
如果它位于您的独立模块中,请添加这些额外的模块:
import { CommonModule } from "@angular/common";
import { FormsModule } from "@angular/forms";
@NgModule({
imports: [CommonModule, FormsModule], // the order can be random now;
...
})
如果它位于您的 中app.module.ts
,请添加以下模块:
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [ FormsModule, BrowserModule ], // order can be random now
...
})
一个简单的演示来证明这个案例。
就我而言,我添加了缺少的导入,即ReactiveFormsModule
.
如果你想对表单输入使用双向数据绑定,你需要在你的 Angular 模块中导入 FormsModule 包。有关更多信息,请参阅此处的 Angular 2 官方教程和表单的官方文档
在 app.module.ts 添加以下行:
import { FormsModule } from '@angular/forms';
[...]
@NgModule({
imports: [
[...]
FormsModule
],
[...]
})
如果双向绑定不起作用,您应该验证以下内容。
在 html 中,应该以这种方式调用 ngModel。不依赖输入元素的其他属性
<input [(ngModel)]="inputText">
确保 FormsModule 被导入到模块文件中
app.modules.ts
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent,
HomeComponent // suppose, this is the component in which you are trying to use two ay binding
],
imports: [
BrowserModule,
FormsModule,
// other modules
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
确保在声明中添加了您尝试使用 ngModel 进行双向绑定的组件。上一点添加的代码 #2
这是使用 ngModel 进行双向绑定所需要做的一切,这已验证到 angular 9
添加FormsModule
您的NgModule
导入(因此ngModel
是 的一部分FormsModule
)。
请注意,它可以通过延迟加载
AppModule
或特性模块延迟加载。
imports: [
...,
FormsModule,
...
]
在 app.module.ts 中导入表单模块。
import { FormsModule} from '@angular/forms';
@NgModule({
declarations: [
AppComponent,
ContactsComponent
],
imports: [
BrowserModule,HttpModule,FormsModule //Add here form module
],
providers: [],
bootstrap: [AppComponent]
})
在 html 中:
<input type="text" name="last_name" [(ngModel)]="last_name" [ngModelOptions]="{standalone: true}" class="form-control">
import { FormsModule } from '@angular/forms';
//<<<< 在这里导入
BrowserModule, FormsModule
//<<<< 在这里
因此,只需查找app.module.ts
或其他模块文件并确保您已FormsModule
导入...
就我而言,我拼错了,我指的是 ngmodel 而不是 ng Model :)希望它有帮助!
预期 - [(ngModel)] 实际 - [(ngmodel)]
在角度 7 中,您必须导入“ReactiveFormsModule”。
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
我通过这个导入解决了这个问题。它会帮助你。
首先导入 FormsModule,然后在您的 component.ts 中使用 ngModel
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule
];
HTML 代码:
<input type='text' [(ngModel)] ="usertext" />
如果即使在导入 formsmodule 后问题仍然存在,请检查您的 Input没有“name”属性,其值等于页面上的另一个输入。
就我而言,在我的应用程序的延迟加载转换期间,我错误地导入了RoutingModule
而不是我的ComponentModule
内部app-routing.module.ts