我的目的是觀察範圍內的模型,並找出舊值和新值之間的差異。
但是,我發現舊值和新值都是從下面的代碼相同。
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}
爲什麼他們一樣的,如果是故意的,那麼爲什麼呢?
這裏是代碼,http://plnkr.co/edit/rfMCF4x6CmVVT957DPSS?p=preview
無論是文檔,是達不到最新的或者是一個錯誤。 'newValue === oldValue'陳述'false',所以我傾向於認爲它是一個錯誤。 –
原來這是一個已知的bug:https://github.com/angular/angular.js/issues/2621 – KayakDave
我認爲$ watchCollection是deep $ watch的快捷方式。看起來不是。 – allenhwkim