From 4f0c1cace1e13c9cf27c1808ba2e11e5e9dff61f Mon Sep 17 00:00:00 2001 From: Pavel Pomerantsev Date: Sun, 25 May 2014 11:53:28 +0400 Subject: [PATCH] docs($rootScope): fix incorrect docs and make them clearer During the first $digest loop after registering a $watch the listener always run, so the example was incorrect (here's a plunk proving it: http://plnkr.co/edit/NRM4UIp9rk28VbylAtvl). Also, I'm making it clearer that if the value doesn't change, the listener doesn't get called on subsequent $digest loops. --- src/ng/rootScope.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/ng/rootScope.js b/src/ng/rootScope.js index 030e0b05c248..f4fed5096c76 100644 --- a/src/ng/rootScope.js +++ b/src/ng/rootScope.js @@ -261,13 +261,17 @@ function $RootScopeProvider(){ expect(scope.counter).toEqual(0); scope.$digest(); - // no variable change - expect(scope.counter).toEqual(0); + // the listener is always called during the first $digest loop after it was registered + expect(scope.counter).toEqual(1); - scope.name = 'adam'; scope.$digest(); + // but now it will not be called unless the value changes expect(scope.counter).toEqual(1); + scope.name = 'adam'; + scope.$digest(); + expect(scope.counter).toEqual(2); + // Using a listener function @@ -632,12 +636,16 @@ function $RootScopeProvider(){ expect(scope.counter).toEqual(0); scope.$digest(); - // no variable change - expect(scope.counter).toEqual(0); + // the listener is always called during the first $digest loop after it was registered + expect(scope.counter).toEqual(1); - scope.name = 'adam'; scope.$digest(); + // but now it will not be called unless the value changes expect(scope.counter).toEqual(1); + + scope.name = 'adam'; + scope.$digest(); + expect(scope.counter).toEqual(2); * ``` * */