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

$watchGroup unexpected behaviour #12452

Closed
@trippytango

Description

@trippytango

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions