【发布时间】:2017-12-01 07:36:48
【问题描述】:
我正在从official site 学习 Angular 4,并与 2-way data binding through ngModel 一起学习。但是,一旦我将 [(ngModel)] 添加到我的组件模板中,我的应用程序就会停止工作,即使 FormsModule 已导入到 module.ts 文件中。组件未加载。
我正在使用 Visual Studio Code。
这是我的 app.component.ts
import { Component } from '@angular/core';
export class Hero {
id: number;
name: string;
}
@Component({
selector: 'app',
template: `
<h1>{{ title }}</h1>
<h2>{{hero.name}} details!</h2>
<div><label>Id:</label> {{hero.id}} </div>
<div>name:<input [(ngModel)]="hero.name" type="text"></div>
`,
styles:[`
.selected{
transition: padding 0.3s;
color: mediumseagreen;
}
`]
})
export class AppComponent {
title = 'Tour Of Heroes';
hero:Hero = {
id:1,
name:"Mr. Invisible"
};
}
这是 app.module.ts
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent, Hero } from './app.component';
@NgModule({
declarations: [
AppComponent,
FormsModule
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
AppComponent 没有加载,只是显示
加载中...
【问题讨论】:
-
错误是什么?
-
你能显示错误信息和一些代码吗?
-
@Sajeetharan:我也遇到了同样的错误。页面只是保持空白。命令行上没有错误。在 Javascript 控制台(在 Chrome 中)中,它显示“无法绑定到 'ngModel',因为它不是 'input' 的已知属性。”
-
对于因此错误而来到这里的任何其他人:本教程解释了错误以及如何修复它。您只需继续学习本教程即可修复此错误。