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

Commit 06baf18

Browse files
authored
docs($rootScope.Scope): clarify $watchGroup "oldValues" argument
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
1 parent 82597fc commit 06baf18

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/ng/rootScope.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,12 @@ function $RootScopeProvider() {
449449
* values are examined for changes on every call to `$digest`.
450450
* - The `listener` is called whenever any expression in the `watchExpressions` array changes.
451451
*
452+
* `$watchGroup` is more performant than watching each expression individually, and should be
453+
* used when the listener does not need to know which expression has changed.
454+
* If the listener needs to know which expression has changed,
455+
* {@link ng.$rootScope.Scope#$watch $watch()} or
456+
* {@link ng.$rootScope.Scope#$watchCollection $watchCollection()} should be used.
457+
*
452458
* @param {Array.<string|Function(scope)>} watchExpressions Array of expressions that will be individually
453459
* watched using {@link ng.$rootScope.Scope#$watch $watch()}
454460
*
@@ -457,7 +463,34 @@ function $RootScopeProvider() {
457463
* The `newValues` array contains the current values of the `watchExpressions`, with the indexes matching
458464
* those of `watchExpression`
459465
* and the `oldValues` array contains the previous values of the `watchExpressions`, with the indexes matching
460-
* those of `watchExpression`
466+
* those of `watchExpression`.
467+
*
468+
* Note that `newValues` and `oldValues` reflect the differences in each **individual**
469+
* expression, and not the difference of the values between each call of the listener.
470+
* That means the difference between `newValues` and `oldValues` cannot be used to determine
471+
* which expression has changed / remained stable:
472+
*
473+
* ```js
474+
*
475+
* $scope.$watchGroup(['v1', 'v2'], function(newValues, oldValues) {
476+
* console.log(newValues, oldValues);
477+
* });
478+
*
479+
* // newValues, oldValues initially
480+
* // [undefined, undefined], [undefined, undefined]
481+
*
482+
* $scope.v1 = 'a';
483+
* $scope.v2 = 'a';
484+
*
485+
* // ['a', 'a'], [undefined, undefined]
486+
*
487+
* $scope.v2 = 'b'
488+
*
489+
* // v1 hasn't changed since it became `'a'`, therefore its oldValue is still `undefined`
490+
* // ['a', 'b'], [undefined, 'a']
491+
*
492+
* ```
493+
*
461494
* The `scope` refers to the current scope.
462495
* @returns {function()} Returns a de-registration function for all listeners.
463496
*/

0 commit comments

Comments
 (0)