Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

$watchCollection old value is not updated properly for multiple updates #16392

Closed
SManorathna opened this issue Jan 7, 2018 · 2 comments
Closed

Comments

@SManorathna
Copy link

I have an html file which has 3 check box values

<input type="checkbox"ng-model="checkbox-1"/>
<input type="checkbox"ng-model="checkbox-2"/>
<input type="checkbox"ng-model="checkbox-3"/>

and there is a watchCollection defined in the controller where it will be triggered when a checkbox status is changed

$scope.$watchGroup(['checkbox-1', 'checkbox-2', 'checkbox-3'], function (newValue, oldValue) {
    console.log('checkbox-1: new - ' + newValue[0] + ' old - ' + oldValue[0]);
    console.log('checkbox-1: new - ' + newValue[1] + ' old - ' + oldValue[1]);
    console.log('checkbox-1: new - ' + newValue[2] + ' old - ' + oldValue[2]);
  });

But when the checkboc values are updated again and again, the old value list of the $watchCollection is not updated properly and it keep populated with the old values.

e.g.
all check boxes are un-tick initially
tick on the 1st checkbox -> oldValue = [false, false, false], newValue = [true, false, false]
tick the 2nd checkbox now -> oldvalue = [false, false, false], newValue = [true, true, false]
In the old value list, the first value should be true since we changed it in the first operation to true. But its still the initial one

I know i can resolve this by updating the check box variables with a model value and adding a watch for model. Would be great anyone have any ideas to resolve this using the $watchCollection approach ?

JSFiddle - https://jsfiddle.net/a0h5nujo/

@frederikprijck
Copy link
Contributor

frederikprijck commented Jan 7, 2018

This was a bug/unexpected behavior which should be fixed by c2b8fab which will be released as part of 1.7.

Duplicate of #16004

@Narretz
Copy link
Contributor

Narretz commented Jan 8, 2018

That's right @frederikprijck , thanks for answering this

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants