Reputation: 443
I was looking online but was unable to turn up anything on it. Does anyone know if there is anyway to have $watch return only the object which had one of its properties change.
Currently having it watch an array of objects so that when a specific property changes it then updates another array's object. Right now i'm iterating through the old and new arrays that $watch passes back and checking to see what changed. Seems wasteful and with the large arrays of data i'm expecting not something i'd prefer.
Upvotes: 0
Views: 170
Reputation: 1534
From AngularJS docs:
$watch(watchExpression, listener, [objectEquality]):
watchExpression
- expression that is evaluated on each $digest cycle. A change in the return value triggers a call to the listener
listener
- callback called whenever the value of watchExpression changes; function(newVal, oldVal, scope)
; params:
newVal
contains the current value of the watchExpressionoldVal
contains the previous value of the watchExpressionscope
refers to the current scopeobjectEquality
- compare for object equality using angular.equals instead of comparing for reference equality
So you could pass in your function newVal
and oldVal
params and check what object from array is popped up/pushe into.
Hope that helps
Upvotes: 1
Reputation: 3623
try with this trick.
$scope.$watch('myArray.length', function(){
console.log("My array has changed",$scope.myArray);
});
Upvotes: 0