When you're using a $watch method (with equality by value). Is there a way to see which object property was changed?
eg
/**
* Save state of button: Cancelled
*/
$scope.$watch('buttons.cancelled', function(newValue, oldValue) {
if(newValue != oldValue && newValue !== undefined) {
privates.viewState.buttons.cancelled = $scope.buttons.cancelled;
}
});
/**
* Save state of button: Booked
*/
$scope.$watch('buttons.booked', function(newValue, oldValue) {
if(newValue != oldValue && newValue !== undefined) {
privates.viewState.buttons.booked = $scope.buttons.booked;
}
});
Turned into;
$scope.$watch('buttons', function(newValue, oldValue) {
if(newValue != oldValue && newValue !== undefined) {
//Is there a way to know which button triggered this watch?
}
}, true);
Here is an example .
I would also like to see an angular built-in feature but as for now You need to do that manually
I put the buttons inside array of objects rather than in hash keys because:
$scope.buttons = [
{ name: 'cancelled', checked: false},
{ name: 'booked', checked: false},
{ name: 'something', checked: false}
];
$scope.$watch('buttons',function(newVal,oldVal){
oldVal = oldVal.reduce(function(p,k){ p[k.name] = k.checked; return p;},{});
var changed = newVal.filter(function(k){
return oldVal[k.name] !== k.checked;
});
changed = changed.reduce(function(p,k){ p[k.name] = k.checked; return p;},{});
$scope.changed = changed;
},true);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.