Angular2 ngModelChange以前的值 [英] Angular2 ngModelChange previous value

查看:459
本文介绍了Angular2 ngModelChange以前的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获取ngModelChange上字段的先前(最后一个)值?
我拥有的东西是这样的

Is there a way to get the previous(last) value of a field on ngModelChange? What I have goes something like this

HTML

<input type="text" [(ngModel)]="text" (ngModelChange)="textChanged($event)">

处理程序

private textChanged(event) {
    console.log('changed', this.text, event);
}

我得到的是

changed *newvalue* *newvalue*

当然我可以使用另一个变量来保留较旧的值,但是还有更好的方法吗?

Of course I can keep the older value using another variable, but is there a better way?

推荐答案

所以发现有点奇怪(至少对于我)可能的解决方案,只需更改相关代码即可。因此,在 [(ngModel)] 之前分配(ngModelChange)属性时,我得到的是使用相同处理程序执行的操作:

So found kinda weird(at least for me) possible solution for this with least changes in the code in question. So on assigning the (ngModelChange) attribute before [(ngModel)] what I get is following with the same handler:

changed *older value* *new value*

我在 this.text 中得到新值,如下所示:

I get the new value in this.textlike so:

setTimeout(() => console.log(this.text), 0);

这篇关于Angular2 ngModelChange以前的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆