有什么方法可以通过 $scope.$watch 检测删除或添加的项目吗?
请看下面的例子:
var data = [{id: 1}, {id: 2}, {id: 3}];
$scope.$watch(function () {return data}, function (newValue, oldValue) {
console.log(newValue, oldValue);
// I want to detect removing or adding here:
// var removedItem = ...?
// var addedItem = ...?
}, true)
data.push({id: 4});
data.splice(0, 1);
谢谢