【发布时间】:2015-05-08 15:59:19
【问题描述】:
我需要观察对象列表中的变化,并对变化执行一些操作,但是,Angular $watch 仅在首次创建列表并将初始对象推入其中时触发一次:
$scope.$watch('components', function(newComponents, oldComponents) {
if (typeof $scope.components !== 'undefined')) {
return;
}
// this alert show only on first change
alert('change');
});
// only pushing first object triggers the $watch
$scope.components.push({ id: 1, name: 'first component'});
$scope.components.push({ id: 2, name: 'second component'});
$scope.components.push({ id: 3, name: 'third component'});
【问题讨论】:
标签: arrays angularjs onchange angularjs-watch