[英]ngModel doesn't work properly in Angular 4
I faced a problem with ngModel
directive.我遇到了
ngModel
指令的问题。 I have an input
<td><input type="text" ([ngModel])="desc"></td>
in which I assigned value of that input
to the variable called desc
.我有一个
input
<td><input type="text" ([ngModel])="desc"></td>
在其中我将该input
值分配给名为desc
的变量。 Then in component
I just want to print it in the console by:然后在
component
我只想通过以下方式在控制台中打印它:
addItem(){
console.log('=======', this.desc);
}
The method addItem()
is decalred in html
file below the input
:方法
addItem()
在input
下方的html
文件中贴标:
<td>
<i
class="fa fa-plus-square add-button"
(click)="addItem()">
</i>
</td>
The thing which I get back is: ======= undefined
in the console.我得到的东西是:
======= undefined
在控制台中======= undefined
。 Can anyone tell me why I got this?谁能告诉我为什么我得到这个? In diffrent place of this app I'm also using
ngModel
and everything works good在这个应用程序的不同地方,我也在使用
ngModel
并且一切正常
The syntax of the data-binding operators ( and [ is wrong, if you want dual binding to work properly, you need to remember the famous sentence "Banana in a box" so you need to write [(ngModel)] it's like a banana in a box [()] .数据绑定运算符 ( 和 [ 是错误的,如果你想让双重绑定正常工作,你需要记住那句名句“Banana in a box”,所以你需要写 [(ngModel)] it's like abanana在盒子里 [()] 。
For information, [ is for view to controller binding and ( for the inverse.有关信息,[ 用于控制器绑定的视图,而 ( 则相反。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.