Angular2 ngModelChange以前的值

编程入门 行业动态 更新时间:2024-10-21 14:27:57
本文介绍了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以前的值

本文发布于:2023-10-28 19:27:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537567.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ngModelChange

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!