angular.scope.$watch

Description

Registers a listener callback to be executed whenever the watchExpression changes.

If you want to be notified whenever $digest is called, you can register an watchExpression function with no listener. (Since watchExpression, can execute multiple times per $digest cycle when a change is detected, be prepared for multiple calls to your listener.)

Example

       var scope = angular.scope();
       scope.name = 'misko';
       scope.counter = 0;

       expect(scope.counter).toEqual(0);
       scope.$watch('name', function(scope, newValue, oldValue) { counter = counter + 1; });
       expect(scope.counter).toEqual(0);

       scope.$digest();
       // no variable change
       expect(scope.counter).toEqual(0);

       scope.name = 'adam';
       scope.$digest();
       expect(scope.counter).toEqual(1);
     

Usage

angular.scope.$watch(watchExpression[, listener]);

Parameters

Returns

{function()}

Returns a deregistration function for this listener.