angular input接收不到值_Angular双向绑定的一个例子

3a424edca452075afcfde0ede4261444.png
name:

> [(ngModel)] is Angular's two-way data binding syntax.

Here it binds the hero.name property to the HTML textbox so that data can flow in both directions: from the hero.name property to the textbox, and from the textbox back to the hero.name.

ngModel是Angular的双向绑定语法,将Angular model的hero.name属性绑定到了HTML input字段上,这样数据流可以在hero.name属性和input字段之间双向传递。

上面的代码会导致如下错误:

> Failed to compile.

src/app/heroes/heroes.component.html:7:14 - error NG8002: Can't bind to 'ngModel' since it isn't a known property of 'input'.

7

~~~~~~~~~~~~~~~~~~~~~~~

src/app/heroes/heroes.component.ts:6:16

6 templateUrl: './heroes.component.html',

~~~~~~~~~~~~~~~~~~~~~~~~~

Error occurs in the template of component HeroesComponent.

f540a110d5b0c3f4326f70890c872864.png

原因是因为缺少FormsModule 的导入。

在app.module.ts里导入FormsModule:

f450ce6ea60800f736e8b7871df715ff.png

再加入@NgModule的imports区域:

68377158272ef5d2268ce2862b93a635.png

最后效果如下:

fde836c4b0defcfb6483580449254c6c.png

只要更改input field里的值,h2区域的值也对应变化了。

aa508aa698bb8e51c053e7fb82a8f806.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值