简体   繁体   English

当ngModelChange之后模型保持不变时,Angular 4重新绑定

[英]angular 4 re-binding when model stays the same after ngModelChange

I have an input 我有输入

<input [(ngModel)]="plot" type="number" step="0.01" (ngModelChange)="onPlotChange($event)" />

onPlotChange(value) {
    this.plot = value;
    this.plot = Math.round(this.plot * 100) / 100;
}

When i enter a number, lets say - 1.956, it instantly changes to 1.96 as it is rounded up, this.plot is different to the value is rebound to the view. 当我输入一个数字时,可以说-1.956,将其四舍五入后立即变为1.96,this.plot不同于该值反弹到视图。

When i enter - 1.951, it stays like that in the view because the model value before the change was 1.95 and after the change it is 1.95 so it doesn't detect any changes and doesn't re-bind the value back to the view so the view stays with .1951. 当我输入-1.951时,它在视图中保持不变,因为更改前的模型值为1.95,更改后为1.95,因此它不会检测到任何更改,也不会将值重新绑定回视图因此,视图保持在.1951。

How do i force angular to re-bind the value to the view? 我如何强制角度将值重新绑定到视图?

http://plnkr.co/kJxF7rixZXQdWRMnVoRd Enter 0.956 - expected result 0.96 Enter 0.951 - expected result 0.95, actual result in view 0.951 http://plnkr.co/kJxF7rixZXQdWRMnVoRd输入0.956-预期结果0.96输入0.951-预期结果0.95,视图中的实际结果0.951

There is an alternate solution to achieve the desired result. 有一种替代解决方案可实现所需的结果。 You can use angular's (keyup) event 您可以使用angular的(keyup)事件

<input [(ngModel)]="plot" type="number" step="0.01" (keyup)="onPlotChange(plot)" />

    onPlotChange(value) {
      this.plot=value;
        this.plot = Math.round(this.plot * 100) / 100;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM