ngModel是否在不使用“香蕉"的情况下创建双向数据绑定?包装纸 [英] does ngModel create two way data binding if used without "banana" wrapping
问题描述
在本文中 Todd Motto解释说:
In this article Todd Motto explains that:
ngModel =如果未分配绑定或值,则ngModel将寻找 名称属性,并将该值作为新的对象键分配给全局 ngForm对象:
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>
关于香蕉"包装:
[(ngModel)] =双向绑定语法,可以从 绑定的组件类,但还要对其进行更新:
[(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>
因此,我期望在他的最后一个示例中看到[(ngModel)]
.但是,他使用了普通的ngModel
指令.它会创建双向数据绑定吗?看来它根本不会创建任何绑定.
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.
推荐答案
它创建了一种绑定到form.value[name]
的方式,在这种情况下,ngModel
只是更新了表单value
对象中的相应属性.
It creates one way binding to form.value[name]
, ngModel
in this case just updates corresponding property in forms value
object.
请参见 https://angular.io/docs/ts/latest/api/forms/index/NgModel-directive.html
这篇关于ngModel是否在不使用“香蕉"的情况下创建双向数据绑定?包装纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!