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

Old values in $watchGroup's callback is not correct. #16004

Closed
1 of 3 tasks
tobyee opened this issue May 22, 2017 · 5 comments
Closed
1 of 3 tasks

Old values in $watchGroup's callback is not correct. #16004

tobyee opened this issue May 22, 2017 · 5 comments

Comments

@tobyee
Copy link
Contributor

tobyee commented May 22, 2017

I'm submitting a ...

  • bug report
  • feature request
  • other (Please do not submit support requests here (see above))

Current behavior:

Old values in $watchGroup's parameters is not correct.

Expected / new behavior:

Correct values.

Minimal reproduction of the problem with instructions:

<input id="input1" ng-model="t1" ng-change="t2=t1">
<input id="input2" ng-model="t2">
<script>
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
  $scope.$watchGroup(['t1', 't2'], function(nvals, ovals) {
    console.log('nvals:' + angular.toJson(nvals) + ', ovals:' + angular.toJson(ovals))
  });
});
</script>

Steps:

  1. initial state
    log: [null,null], ovals:[null,null]
  2. type "a" in input1
    log: nvals:["a","a"], ovals:[null,null]
  3. type "b" in input2
    log: nvals:["a","b"], ovals:[null,"a"] (??? ovals expect to be ["a","a"] but not.)

plunker:
https://plnkr.co/edit/slDfDzwvGT6jrZtn42C9?p=preview

Angular version: 1.6.4

Browser: all

Anything else:

@Narretz
Copy link
Contributor

Narretz commented May 22, 2017

I think this works as egpected. Watch group old / new values are tracked individually, not as a collection. The first value didn't change when you changed the second value, it only changed from null to a, so it's correct that when the second value changed to b, the old value of the first is still null.

@tobyee
Copy link
Contributor Author

tobyee commented May 22, 2017

I found some similar issues:
#8671
#12452

So it's by design behavior...
Even if the design is reasonable technically, it is weird result that confuse with the behavior of $watch.

I also found a PR #12643 that try to clarify the behavior, should it be merge?

@Narretz
Copy link
Contributor

Narretz commented May 22, 2017

@tobyee thanks for unearthing the issues and PR. I will create a new PR, as I think the one you linked doesn't improve the explanation sufficiently.

@Narretz Narretz self-assigned this May 22, 2017
Narretz added a commit to Narretz/angular.js that referenced this issue May 22, 2017
Narretz added a commit to Narretz/angular.js that referenced this issue Jun 29, 2017
Narretz added a commit to Narretz/angular.js that referenced this issue Jun 29, 2017
@gkalpak
Copy link
Member

gkalpak commented Jun 29, 2017

Closing since this has been changed in master (1.7+) with #15854 and there is a PR for documenting it for 1.6.x (#16005).

@gkalpak gkalpak closed this as completed Jun 29, 2017
@gkalpak
Copy link
Member

gkalpak commented Jun 29, 2017

See also #16024 for a discussion on the different behaviors.

Narretz added a commit that referenced this issue Jun 29, 2017
This should help to prevent issues such as #8671, #12452, #16004.

Note that the behavior will change in 1.7 (see #15854)

Closes #12643
Closes #16005
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