I am trying to watch a simple object on $scope, but I am confused as to why the following always outputs state:{value} for both the country and state select elements.
angular.module('app').controller('FiltersCtrl', ['$scope', 'filterRepo', function ($scope, filterRepo) {
$scope.data = {
lookups: {
countries: [
{ id: 1, name: 'USA' },
{ id: 2, name: 'Canada' },
{ id: 3, name: 'Mexico' }
],
states: [
{ id: 1, name: 'Oregon' },
{ id: 2, name: 'Texas' },
{ id: 3, name: 'Iowa' }
]
}
};
$scope.query = {
filter: {
country: 1,
state: 1
}
};
for (var item in $scope.query.filter) {
$scope.$watch('query.filter.' + item, function (newValue, oldValue) {
if (newValue !== oldValue) {
console.log(item + ' : ' + newValue);
}
}, false);
}
}]);
itemsafely inside the$watchcallback, as its value will have changed to the last possible key (state) when the callback is run.