Angular ngModel数据绑定不工作
我正在学习《英雄之旅》教程,无法理解为什么ngModel没有更新hero.name,或者只是没有更新视图。我在输入中键入,但视图中显示的名称保持不变(暴风) 请让我知道我做错了什么 英雄.component.htmlAngular ngModel数据绑定不工作,angular,typescript,Angular,Typescript,我正在学习《英雄之旅》教程,无法理解为什么ngModel没有更新hero.name,或者只是没有更新视图。我在输入中键入,但视图中显示的名称保持不变(暴风) 请让我知道我做错了什么 英雄.component.html <h2>{{ hero.name | uppercase }}</h2> <div><span>id: </span>{{ hero.id }}</div> <div> <label>
<h2>{{ hero.name | uppercase }}</h2>
<div><span>id: </span>{{ hero.id }}</div>
<div>
<label>name
<input ([ngModel])="hero.name" placeholder="name">
</label>
</div>
hero.ts
import { Component, OnInit } from '@angular/core';
import { Hero } from '../hero';
@Component({
selector: 'app-heroes',
templateUrl: './heroes.component.html',
styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit {
hero: Hero = {
id: 1,
name: 'Windstorm'
};
constructor() { }
ngOnInit() {
}
}
export class Hero {
id: number;
name: string;
}
([ngModel])
应该是[(ngModel)]
。它被称为“盒子里的香蕉”符号;)()([ngModel])
应该是[(ngModel)]
。它被称为“盒子里的香蕉”符号;)()打开AppModule(app.module.ts)并从@angular/forms库导入FormsModule符号
import { FormsModule } from '@angular/forms'; // <-- NgModel lives here
当浏览器刷新时,应用程序应再次工作。您可以编辑英雄的姓名,并立即在上面的文本框中看到反映的更改
来源:打开AppModule(app.module.ts)并从@angular/forms库导入FormsModule符号
import { FormsModule } from '@angular/forms'; // <-- NgModel lives here
当浏览器刷新时,应用程序应再次工作。您可以编辑英雄的姓名,并立即在上面的文本框中看到反映的更改
来源:你试过了吗<代码>[(ngModel)]您试过了吗<代码>[(ngModel)]我担心是这样的。谢谢我担心是这样的。谢谢