Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs watchcollection previous value

Tags:

angularjs

Update

This issue has been resolved in 1.3.0 beta: see the commit


I have an array I'd like to listen for changes on and compare the new value to the old value. The docs for $watchCollection (http:// docs.angularjs.org/api/ng.$rootScope.Scope), say that the callback takes three parameters, the first one being the new collection and the second being a copy of the former value. It seems that the same array is being passed to both parameters, instead of the new and old values.

In this example, the same array is passed to the new value and former value, whether I'm pushing values onto the array, setting the variable to a new array, or editing an element of the array. All 3 examples trigger the watch listener, but none contain the former value in the callback.

I am able to get the results I want with this example. It uses the $watch(obj, listener, equality) method with true as the third parameter. Shouldn't $watchCollection provide this functionality as well?

$scope.$watchCollection("arr", function(newVal, oldVal) {
    //here, oldVal should contain a copy of the former value,
    //but it is the same as newVal
});
like image 682
David Ferretti Avatar asked Sep 09 '26 07:09

David Ferretti


1 Answers

EDIT: Sorry about the previous answer.

But it seems it's a open issue in the AngularJs repository with the exact same problem.

https://github.com/angular/angular.js/issues/2621

Best of luck.

like image 113
Lucas Lazaro Avatar answered Sep 12 '26 05:09

Lucas Lazaro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!