forked from angular/code.angularjs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular.compile.html
103 lines (88 loc) · 5.87 KB
/
angular.compile.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<h1>angular.compile</h1>
<div class="angular-compile"><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>
<div class="description"><p>Compiles a piece of HTML string or DOM into a template and produces a template function, which
can then be used to link <a href="#!/api/angular.scope"><code>scope</code></a> and the template together.</p>
<p>The compilation is a process of walking the DOM tree and trying to match DOM elements to
<a href="#!/api/angular.markup"><code>markup</code></a>, <a href="#!/api/angular.attrMarkup"><code>attrMarkup</code></a>,
<a href="#!/api/angular.widget"><code>widgets</code></a>, and <a href="#!/api/angular.directive"><code>directives</code></a>. For each match it
executes corresponding markup, attrMarkup, widget or directive template function and collects the
instance functions into a single template function which is then returned.</p>
<p>The template function can then be used once to produce the view or as it is the case with
<a href="#!/api/angular.widget.@ng:repeat"><code>repeater</code></a> many-times, in which case each call results in a view
that is a DOM clone of the original template.</p><div ng:non-bindable><pre class="brush: js; html-script: true;">
// compile the entire window.document and give me the scope bound to this template.
var rootScope = angular.compile(window.document)();
// compile a piece of html
var rootScope2 = angular.compile('<div ng:click="clicked = true">click me</div>')();
// compile a piece of html and retain reference to both the dom and scope
var template = angular.element('<div ng:click="clicked = true">click me</div>'),
scope = angular.compile(template)();
// at this point template was transformed into a view
</pre></div></div>
<h2>Usage</h2>
<div class="usage"><div ng:non-bindable=""><pre class="brush: js; html-script: true;">angular.compile(element);</pre>
</div>
<h3>Parameters</h3>
<ul class="parameters"><li><code ng:non-bindable="">element – {string|DOMElement} – </code>
<p>Element or HTML to compile into a template function.</p></li>
</ul>
<h3>Returns</h3>
<div class="returns"><code ng:non-bindable="">{function([scope][, cloneAttachFn])}</code>
– <p>a template function which is used to bind template
(a DOM element/tree) to a scope. Where:</p>
<ul>
<li><code>scope</code> - A <a href="#!/api/angular.scope"><code>Scope</code></a> to bind to. If none specified, then a new
root scope is created.</li>
<li><p><code>cloneAttachFn</code> - If <code>cloneAttachFn</code> is provided, then the link function will clone the
<code>template</code> and call the <code>cloneAttachFn</code> function allowing the caller to attach the
cloned elements to the DOM document at the appropriate place. The <code>cloneAttachFn</code> is
called as: <br/> <code>cloneAttachFn(clonedElement, scope)</code> where:</p>
<ul><li><code>clonedElement</code> - is a clone of the original <code>element</code> passed into the compiler.</li>
<li><code>scope</code> - is the current scope with which the linking function is working with.</li></ul></li>
</ul>
<p>Calling the template function returns the scope to which the element is bound to. It is either
the same scope as the one passed into the template function, or if none were provided it's the
newly create scope.</p>
<p>If you need access to the bound view, there are two ways to do it:</p>
<ul>
<li>If you are not asking the linking function to clone the template, create the DOM element(s)
before you send them to the compiler and keep this reference around.</li>
</ul><div ng:non-bindable><pre class="brush: js; html-script: true;">
var view = angular.element('<p>{{total}}</p>'),
scope = angular.compile(view)();
</pre></div><ul>
<li>if on the other hand, you need the element to be cloned, the view reference from the original
example would not point to the clone, but rather to the original template that was cloned. In
this case, you can access the clone via the cloneAttachFn:</li>
</ul><div ng:non-bindable><pre class="brush: js; html-script: true;">
var original = angular.element('<p>{{total}}</p>'),
scope = someParentScope.$new(),
clone;
angular.compile(original)(scope, function(clonedElement, scope) {
clone = clonedElement;
//attach the clone to DOM document at the right place
});
//now we have reference to the cloned DOM via `clone`
</pre></div><p>Compiler Methods For Widgets and Directives:</p>
<p>The following methods are available for use when you write your own widgets, directives,
and markup. (Recall that the compile function's this is a reference to the compiler.)</p>
<p><code>compile(element)</code> - returns linker -
Invoke a new instance of the compiler to compile a DOM element and return a linker function.
You can apply the linker function to the original element or a clone of the original element.
The linker function returns a scope.</p>
<ul>
<li><p><code>comment(commentText)</code> - returns element - Create a comment element.</p></li>
<li><p><code>element(elementName)</code> - returns element - Create an element by name.</p></li>
<li><p><code>text(text)</code> - returns element - Create a text element.</p></li>
<li><p><code>descend([set])</code> - returns descend state (true or false). Get or set the current descend
state. If true the compiler will descend to children elements.</p></li>
<li><p><code>directives([set])</code> - returns directive state (true or false). Get or set the current
directives processing state. The compiler will process directives only when directives set to
true.</p></li>
</ul>
<p>For information on how the compiler works, see the
<a href="#!/guide/dev_guide.compiler">Angular HTML Compiler</a> section of the Developer Guide.</p></div>
</div>
</div>