Registers listener
as a callback to be executed every time the watchExp
changes. Be aware
that callback gets, by default, called upon registration, this can be prevented via the
initRun
parameter.
var scope = angular.scope(); scope.name = 'misko'; scope.counter = 0; expect(scope.counter).toEqual(0); scope.$watch('name', 'counter = counter + 1'); expect(scope.counter).toEqual(1); scope.$eval(); expect(scope.counter).toEqual(1); scope.name = 'adam'; scope.$eval(); expect(scope.counter).toEqual(2);
angular.scope.$watch(watchExp, listener, exceptionHanlder, initRun);
watchExp – {function()|string} –
Expression that should be evaluated and checked for
change during each eval cycle. Can be an angular string expression or a function.listener – {function()|string} –
Function (or angular string expression) that gets called
every time the value of the watchExp
changes. The function will be called with two
parameters, newValue
and oldValue
.exceptionHanlder(optional=angular.service.$exceptionHandler) – {(function()|DOMElement)} –
Handler
that gets called when watchExp
or listener
throws an exception. If a DOMElement is
specified as handler, the element gets decorated by angular with the information about the
exception.initRun(optional=true) – {boolean} –
Flag that prevents the first execution of the listener upon
registration.