我需要注意对象列表中的更改,并对更改执行一些操作,但是,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'});