mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-13 08:56:40 +08:00
docs(rootScope): add example of using a listener function for $watch
Closes #4451
This commit is contained in:
committed by
Pete Bacon Darwin
parent
1ae34aac81
commit
459e85bfea
@@ -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>
|
||||
*
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user