【问题标题】:Cannot update ngModel value from component.ts more than once无法多次从 component.ts 更新 ngModel 值
【发布时间】:2023-03-18 04:33:02
【问题描述】:

我在 html 中有一个输入字段,我想在对值执行一些工作后在输入内容后立即清除它。 我为此使用 ngModel。 问题是我的代码仅在我第一次在其中写入内容时才清除输入字段的值。之后就不行了。

receive.component.html

<mat-form-field class="example-full-width">
  <input [(ngModel)]="donation_detail"  (ngModelChange)="fecthDetail()" 
  matInput placeholder=" code">
</mat-form-field>

receive.component.ts

fecthDetail(){
     console.log(this.donation_detail)
     //do something with the vlaue and then clear it
     this.donation_detail = '';
}

【问题讨论】:

    标签: angular input ngmodel two-way-binding


    【解决方案1】:

    当您在&lt;input&gt; 中添加(ngModelChange) 时,ngModel 中不需要更改事件绑定

    <mat-form-field class="example-full-width">
      <input [ngModel]="donation_detail"  (ngModelChange)="fecthDetail()" 
      matInput placeholder=" code">
    </mat-form-field>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-27
      • 2021-09-15
      • 2022-12-20
      相关资源
      最近更新 更多