要将输入的值绑定(bind)到属性,我们使用 ngModel 指令。例如:

<input type='text' [(ngModel)]='model' />

为什么我们不能简单地在 input 元素的 value 属性上使用绑定(bind)?
<input type='text' [(value)]='model' />

最佳答案

你可以做

<input type='text' [value]='model' (input)="model=$event" />
[(value)]='model' 不起作用,因为 <input> 不发出 valueChange 事件。
ngModel 还提供了直接值绑定(bind)所没有的表单集成。

另见 https://angular.io/docs/ts/latest/guide/template-syntax.html#!#two-way
ngModel 使用提供的 ControlValueAccessor s,它们是为各种输入元素提供的指令(也可以是您自己的组件的自定义元素),充当 ngModel 和任何组件之间的适配器。这是为了统一绑定(bind)各种组件和输入元素。

另见 https://github.com/angular/angular/blob/2.4.8/modules/%40angular/forms/src/directives/checkbox_value_accessor.ts#L17-L50

关于angularjs - 直接绑定(bind)到 value 属性而不是 ngModel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42779231/

10-15 00:06