0
我想观察$ scope上的一个简单对象,但我很困惑,为什么以下总是为国家和州选择元素输出state:{value}
。
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);
}
}]);
你不能安全的'$ watch'回调内部使用'item',因为它的价值将发生变化到最后当回调运行时可能的键('状态')。 –
小提琴http://jsfiddle.net/E62VE/ – PixnBits
@still_learning那么我如何获得对已更改对象的引用? – Sam