我的意图是在范围内观看一个模型,并找出旧价值与新价值之间的差异。
但是,我发现旧的值和新的值都是从以下代码相同的。
app.controller('MyCtrl',function($scope,$timeout){ $scope.markers = {}; $scope.$watchCollection('markers',function(newValue,oldValue){ console.log('being watched oldValue:',oldValue,'newValue:',newValue); }); $timeout( function() { $scope.markers.foo = 1; },500); $timeout( function() { $scope.markers.bar = 2; },500); });
输出:
being watched oldValue: Object {} newValue: Object {} script.js:6 being watched oldValue: Object {foo: 1} newValue: Object {foo: 1} script.js:6 being watched oldValue: Object {foo: 1,bar: 2} newValue: Object {foo: 1,bar: 2}
为什么他们是一样的,如果是故意的,那为什么?