ng-change get new value and original value ng-change get new value and original value angularjs angularjs

ng-change get new value and original value


With an angular {{expression}} you can add the old user or user.id value to the ng-change attribute as a literal string:

<select ng-change="updateValue(user, '{{user.id}}')"         ng-model="user.id" ng-options="user.id as user.name for user in users"></select>

On ngChange, the 1st argument to updateValue will be the new user value, the 2nd argument will be the literal that was formed when the select-tag was last updated by angular, with the old user.id value.


Also you can use

<select ng-change="updateValue(user, oldValue)"            ng-init="oldValue=0"       ng-focus="oldValue=user.id"       ng-model="user.id" ng-options="user.id as user.name for user in users"></select>


Just keep a currentValue variable in your controller that you update on every change. You can then compare that to the new value every time before you update it.'

The idea of using a watch is good as well, but I think a simple variable is the simplest and most logical solution.