问题
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