does ngModel create two way data binding if used without “banana” wrapping

孤者浪人 提交于 2019-12-24 06:21:10

问题


In this article Todd Motto explains that:

ngModel = if no binding or value is assigned, ngModel will look for a name attribute and assign that value as a new Object key to the global ngForm Object:

<form novalidate #f="ngForm">
  ...
    <input
     type="text"
     placeholder="Your full name"
     ngModel>
  ...
</form>

And about "banana" wrapping:

[(ngModel)] = two-way binding syntax, can set initial data from the bound component class, but also update it:

<form #f="ngForm">
  ...
    <input
      type="text"
      placeholder="Your full name"
      name="name"
      [(ngModel)]="user.name">
  ...
</form>

So I was expecting to see [(ngModel)] used in his final example. However, he used the plain ngModel directive. Does it create two-way data-binding? It seems that it doesn't create any binding at all.


回答1:


It creates one way binding to form.value[name], ngModel in this case just updates corresponding property in forms value object.

See https://angular.io/docs/ts/latest/api/forms/index/NgModel-directive.html



来源:https://stackoverflow.com/questions/42191427/does-ngmodel-create-two-way-data-binding-if-used-without-banana-wrapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!