Reputation: 4886
I have the following watch in my controller.
var pageFunc = $scope.$watch('page["USER"].number', function(newob,oldob){
console.log("WATCHING page");
if(oldob != newob){
//perform data load
}
},true);
my collection looks like this
page["USER"].number = 20;
page["TESTER"].number = 60;
page["BORROWER"].number = 30;
page["CLIENT"].number = 80;
I need a single watch to watch all these collection changes for a particular element.
I have tried something like this.
$scope.$watch('page[" '+$scope.selectedType+'"].number', function(newob,oldob)
where $scope.selectedType is set inside ng-init.
but since watch is triggered before ng-init, this fails. What is the best way to achieve this?
Upvotes: 0
Views: 32
Reputation: 30098
You can watch its changes using a function callback:
$scope.$watch(function() {
return page[$scope.selectedType].number;
}, function(newValue, oldValue) {
if(oldValue != newValue) {
// perform something
}
});
Alternatively, if $scope.selectedType
is a unique value, then I guess you can safely watch for it instead:
$scope.$watch('selectedType', function(newValue, oldValue) {
if(oldValue != newValue) {
// perform something
}
});
Upvotes: 1