forked from angular/code.angularjs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular.scope.$tryEval.html
48 lines (42 loc) · 1.81 KB
/
angular.scope.$tryEval.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
<h1>angular.scope.$tryEval</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>Evaluates the expression in the context of the current scope just like
<a href="#!angular.scope.$eval()"><code>angular.scope.$eval()</code></a> with expression parameter, but also wraps it in a try/catch
block.</p>
<p>If exception is thrown then <code>exceptionHandler</code> is used to handle the exception.</p>
<h2>Example</h2><div ng:non-bindable><pre class="brush: js; html-script: true;">
var scope = angular.scope();
scope.error = function(){ throw 'myerror'; };
scope.$exceptionHandler = function(e) {this.lastException = e; };
expect(scope.$eval('error()'));
expect(scope.lastException).toEqual('myerror');
this.lastException = null;
expect(scope.$eval('error()'), function(e) {this.lastException = e; });
expect(scope.lastException).toEqual('myerror');
var body = angular.element(window.document.body);
expect(scope.$eval('error()'), body);
expect(body.attr('ng-exception')).toEqual('"myerror"');
expect(body.hasClass('ng-exception')).toEqual(true);
</pre></div>
<h2>Usage</h2>
<tt ng:non-bindable>
angular.scope.$tryEval(expression, exceptionHandler);
</tt>
<h3>Parameters</h3>
<ul>
<li><tt>expression</tt> –
<tt>{string|function()}</tt>
<tt></tt>
– Angular expression to evaluate.</li>
<li><tt>exceptionHandler</tt> –
<tt>{function()|DOMElement}</tt>
<tt></tt>
– Function to be called or DOMElement to be
decorated.</li>
</ul>
<h3>Returns</h3>
<tt>{*}</tt> The result of <code>expression</code> evaluation.