Ответ 1
Ниже приведены некоторые варианты использования $watchCollection
http://jsbin.com/IYofiPi/4 - рабочий пример здесь.
Проверьте свой консоль .log, чтобы увидеть событие, которое было запущено.
Просмотр элементов массива
$scope.cities = ['Salvador', 'London', 'Zurich', 'Rio de Janeiro']; //Array
$scope.$watchCollection('cities', function(newValues, oldValues) {
console.log('*** Watched has been fired. ***');
console.log('New Names :', newValues);
});
Наблюдение свойств объекта
$scope.city = {
name: 'London',
country: 'England',
population: 8.174
}
$scope.$watchCollection('city', function(newValues, oldValues) {
console.log('*** Watched has been fired. ***');
console.log('New Names :', newValues);
});
Просмотр списка переменных областей ($ scope.firstPlanet, $scope.secondPlanet)
$scope.firstPlanet = 'Earth';
$scope.secondPlanet = 'Mars';
$scope.$watchCollection('[firstPlanet, secondPlanet]', function(newValues){
console.log('*** Watched has been fired. ***');
console.log('New Planets :', newValues[0], newValues[1]);
});
Начиная с AngularJS 1.3 существует новый метод, называемый $watchGroup для наблюдения набора выражений.