Mat 输入两种方式绑定到值属性不起作用

问题描述 投票:0回答:2

我创建了一个 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>

为什么绑定的属性没有更新?

angular angular6
2个回答
35
投票

使用

[(ngModel)]
代替
[(value)]
(有关演示,请参阅 this stackblitz)。

<input matInput placeholder="Input" [(ngModel)]="currentValue">

本文解释了如何通过组合

[value]
(input)
来获得等效行为。


© www.soinside.com 2019 - 2024. All rights reserved.