mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-16 23:30:23 +00:00
docs(rootScope): add example of using a listener function for $watch
Closes #4451
This commit is contained in:
parent
1ae34aac81
commit
459e85bfea
1 changed files with 32 additions and 0 deletions
|
|
@ -240,6 +240,8 @@ function $RootScopeProvider(){
|
|||
* can compare the `newVal` and `oldVal`. If these two values are identical (`===`) then the
|
||||
* listener was called due to initialization.
|
||||
*
|
||||
* The example below contains an illustration of using a function as your $watch listener
|
||||
*
|
||||
*
|
||||
* # Example
|
||||
* <pre>
|
||||
|
|
@ -261,6 +263,36 @@ function $RootScopeProvider(){
|
|||
scope.name = 'adam';
|
||||
scope.$digest();
|
||||
expect(scope.counter).toEqual(1);
|
||||
|
||||
|
||||
|
||||
// Using a listener function
|
||||
var food;
|
||||
scope.foodCounter = 0;
|
||||
expect(scope.foodCounter).toEqual(0);
|
||||
scope.$watch(
|
||||
// This is the listener function
|
||||
function() { return food; },
|
||||
// This is the change handler
|
||||
function(newValue, oldValue) {
|
||||
if ( newValue !== oldValue ) {
|
||||
// Only increment the counter if the value changed
|
||||
scope.foodCounter = scope.foodCounter + 1;
|
||||
}
|
||||
}
|
||||
);
|
||||
// No digest has been run so the counter will be zero
|
||||
expect(scope.foodCounter).toEqual(0);
|
||||
|
||||
// Run the digest but since food has not changed cout will still be zero
|
||||
scope.$digest();
|
||||
expect(scope.foodCounter).toEqual(0);
|
||||
|
||||
// Update food and run digest. Now the counter will increment
|
||||
food = 'cheeseburger';
|
||||
scope.$digest();
|
||||
expect(scope.foodCounter).toEqual(1);
|
||||
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
|
|
|
|||
Loading…
Reference in a new issue