forked from angular/code.angularjs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular.scope.$watch.html
57 lines (52 loc) · 2.2 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<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>
<h2>Example</h2><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>
<tt ng:non-bindable>
angular.scope.$watch(watchExp, listener, exceptionHanlder, initRun);
</tt>
<h3>Parameters</h3>
<ul>
<li><tt>watchExp</tt> –
<tt>{function()|string}</tt>
<tt></tt>
– Expression that should be evaluated and checked for
change during each eval cycle. Can be an angular string expression or a function.</li>
<li><tt>listener</tt> –
<tt>{function()|string}</tt>
<tt></tt>
– 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><tt>exceptionHanlder</tt> –
<tt>{(function()|DOMElement)=}</tt>
<tt>[angular.service.$exceptionHandler]</tt>
– 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><tt>initRun</tt> –
<tt>{boolean=}</tt>
<tt>[true]</tt>
– Flag that prevents the first execution of the listener upon
registration.</li>
</ul>