我创建了一个 Mat 输入控件,并对其值属性进行了 2 路绑定到我的控制器上的属性,但是当我输入输入时,绑定的属性不会更新。
Stack blitz 链接:https://stackblitz.com/edit/angular-7ojsjo
<div class="example-container">
<mat-form-field>
<input matInput placeholder="Input" [(value)]="currentValue">
</mat-form-field>
<h1>{{currentValue}}</h1>
</div>
为什么绑定的属性没有更新?
使用
[(ngModel)]
代替 [(value)]
(有关演示,请参阅 this stackblitz)。
<input matInput placeholder="Input" [(ngModel)]="currentValue">
本文解释了如何通过组合
[value]
和 (input)
来获得等效行为。