Angular 2 ngModelChange旧值 [英] Angular 2 ngModelChange old value

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

问题描述

有人可以告诉我比较ngModel新旧值的最佳做法是什么吗?

Can someone please tell me what is the best practice for comparing ngModel old and new value?

在角度1:

$scope.$watch('someProperty', funciton(oldVal, newVal){
    // code goes here
})

我之所以这样问,是因为(ngModelChange)从来没有带给我oldVal,只有我带了newVal.

I am asking this because (ngModelChange) never brings me the oldVal , only the newVal.

在我的情况下,我在<select>标记中使用ngModel,并将旧选择与新选择进行比较:

In my case, I am using ngModel in a <select> tag and compare the old selection with the new one:

<select [(ngModel)]="current" (ngModelChange)="onModelChange($event)">
     <option *ngFor="let item of myArray" [ngValue]="item">{{item.name}} </option>
</select>

推荐答案

这可能有效

(ngModelChange)="onModelChange(oldVal, $event); oldVal = $event;"

(ngModelChange)="onModelChange($event)"

oldValue:string;
onModelChange(event) {
  if(this.oldValue != event) {
    ...
  }
  this.oldValue = event;
}

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

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