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

$watchGroup unexpected behaviour #12452

Closed
trippytango opened this issue Jul 28, 2015 · 2 comments
Closed

$watchGroup unexpected behaviour #12452

trippytango opened this issue Jul 28, 2015 · 2 comments

Comments

@trippytango
Copy link

I've been using $watchGroup to watch a range of fields and trigger a range of functions depending if a particular field has been changed.

I've set up the following plnkr to demonstrate the unexpected behaviour I've came across.

 $scope.$watchGroup(['first', 'second', 'third'], function(newValues, oldValues)
  {
    var message =
    {
      first: newValues[0],
      second: newValues[1],
      third: newValues[2],
      firstOld: oldValues[0],
      secondOld: oldValues[1],
      thirdOld: oldValues[2]
    };

    if(newValues[0] !== oldValues[0]){
      console.log('First changed')
    }

    if(newValues[1] !== oldValues[1]){
      console.log('Second changed')
    } 

    if(newValues[2] !== oldValues[2]){
      console.log('Third changed')
    }

    $scope.messages.push(message);
  });

The scenario involves three watched fields and I'd like to trigger a function depending on which field has changed. I've been using the 'newValues' and 'oldValues' to monitor which field has changed.

The problem I've came across is that if I've changed the "Second" field then go and change the "First" or "Third" field, the "Second" function is triggered as its storing the previous 'newValues' and 'oldValues' which makes it look like the "Second" field has changed as demonstrated in this image.

I've highlighted the anomaly in the picture. I'd expect once I started changing the "Third" field, the 'newValues' and 'oldValues' for "Second" to be the same as it isn't the field changing.

The angular documentation for $watchGroup states that watchExpressions is an "Array of expressions that will be individually watched using $watch()". Which makes me think that this isn't intended functionality.

Any clarification if this is a bug or intended functionality would be appreciated.

@Narretz
Copy link
Contributor

Narretz commented Aug 3, 2015

Hm, I think this is indeed the expected behavior. As each expression is considered independently, the new / old values will always reflect the last time the expression has changed. I don't know how difficult it would be to change this - it could also be that there's a performance impact, as you would need to set every expression except that ones that changed newValue === oldValue every time an expression changes.

@Narretz
Copy link
Contributor

Narretz commented Aug 8, 2015

I'm closing this due to lack of response.

@Narretz Narretz closed this as completed Aug 8, 2015
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
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

2 participants