forked from angular/code.angularjs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular.scope.$watch.html
43 lines (39 loc) · 2.18 KB
/
angular.scope.$watch.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<h1>angular.scope.$watch</h1>
<fieldset class="workInProgress"><legend>Work in Progress</legend>
This page is currently being revised. It might be incomplete or contain inaccuracies.</fieldset>
<h2>Description</h2>
<p>Registers <code>listener</code> as a callback to be executed every time the <code>watchExp</code> changes. Be aware
that callback gets, by default, called upon registration, this can be prevented via the
<code>initRun</code> parameter.</p>
<h3>Example</h3><div ng:non-bindable><pre class="brush: js; html-script: true;">
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);
</pre></div><h2>Usage</h2>
<div ng:non-bindable=""><pre class="brush: js; html-script: true;">angular.scope.$watch(watchExp, listener, exceptionHanlder, initRun);</pre>
</div>
<h3>Parameters</h3>
<ul class="parameters"><li><code ng:non-bindable="">watchExp – {function()|string} – </code>
Expression that should be evaluated and checked for
change during each eval cycle. Can be an angular string expression or a function.</li>
<li><code ng:non-bindable="">listener – {function()|string} – </code>
Function (or angular string expression) that gets called
every time the value of the <code>watchExp</code> changes. The function will be called with two
parameters, <code>newValue</code> and <code>oldValue</code>.</li>
<li><code ng:non-bindable="">exceptionHanlder<i>(optional=angular.service.$exceptionHandler)</i> – {(function()|DOMElement)} – </code>
Handler
that gets called when <code>watchExp</code> or <code>listener</code> throws an exception. If a DOMElement is
specified as handler, the element gets decorated by angular with the information about the
exception.</li>
<li><code ng:non-bindable="">initRun<i>(optional=true)</i> – {boolean} – </code>
Flag that prevents the first execution of the listener upon
registration.</li>
</ul>