你可以观察你的变量同时拥有 newValue 和 oldValue。
<input type="text" id="exampleab" ng-model="a.b" >
在您的控制器中:
app.controller('ctrl', function ($scope) {
$scope.$watch('a.b', function (newValue, oldValue) {
console.log('oldValue=' + oldValue);
console.log('newValue=' + newValue);
//do something
});
});
JSFiddle
编辑
您在编辑中提到了一个新要求,所以我编辑了我的答案。你不应该使用 ng-change。您应该在控件获得焦点时获取 oldValue 并将其保存在变量中,然后在 blur 事件中获取新值。
我设置了一个新的小提琴。
在您的控制器中:
app.controller('ctrl', function ($scope) {
$scope.showValues = function () {
alert('oldValue = ' + $scope.oldValue);
alert('newValue = ' + $scope.a.b);
}
});
我你的看法
<input type="text" id="exampleab" ng-model="a.b" ng-init="oldValue = ''" ng-focus="oldValue = a.b" ng-blur="showValues()" />{{a.b}}