forked from angular/code.angularjs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev_guide.compiler.widgets.creating_widgets.html
76 lines (69 loc) · 3.28 KB
/
dev_guide.compiler.widgets.creating_widgets.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
<h1>Developer Guide: Angular HTML Compiler: Widgets: Creating Custom Widgets</h1>
<div class="developer-guide-angular-html-compiler-widgets-creating-custom-widgets"><fieldset class="workInProgress"><legend>Work in Progress</legend>
This page is currently being revised. It might be incomplete or contain inaccuracies.</fieldset>
<p>When you create your own widgets, you must set up your own namespace for them. (See
dev_guide.bootstrap Initializing Angular} for information about namespaces in angular.)</p>
<p>Let's say we would like to create a new element type in the namespace <code>my</code> that can watch an
expression and <code>alert()</code> the user with each new value:</p><div ng:non-bindable><pre class="brush: js; html-script: true;">
// An element widget
<my:watch exp="name"></my:watch>
</pre></div><p>You can implement <code>my:watch</code> like this:</p><div ng:non-bindable><pre class="brush: js;">
angular.widget('my:watch', function(compileElement) {
var compiler = this;
var exp = compileElement.attr('exp');
return function(linkElement) {
var currentScope = this;
currentScope.$watch(exp, function(value){
alert(value);
});
};
});
</pre></div><h2>Creating a Custom Attribute Widget</h2>
<p>Let's implement the same widget as in the example in Defining an Element Widget, but this time as
an attribute that can be added to any existing DOM element:</p><div ng:non-bindable><pre class="brush: js; html-script: true;">
// An attribute widget (my:watch) in a div tag
<div my:watch="name">text</div>
</pre></div><p>You can implement <code>my:watch</code> attribute like this:</p><div ng:non-bindable><pre class="brush: js;">
angular.widget('@my:watch', function(expression, compileElement) {
var compiler = this;
return function(linkElement) {
var currentScope = this;
currentScope.$watch(expression, function(value) {
alert(value);
});
};
});
</pre></div><h2>Live Example of a Custom Element Widget</h2><doc:example>
<pre class="doc-source">
<script>
angular.widget('my:time', function(compileElement){
compileElement.css('display', 'block');
return function(linkElement){
function update(){
linkElement.text('Current time is: ' + new Date());
setTimeout(update, 1000);
}
update();
};
});
</script>
<my:time></my:time>
</pre>
<pre class="doc-scenario">
</pre>
</doc:example><h2>Additional Compiler Methods for Custom Widgets</h2>
<p>The angular compiler exposes methods that you may need to use of when writing your own widgets and
directives. For example, the <code>descend()</code> method lets you control whether the compiler ignores or
processes child elements of the element it is compiling. For information on this and other
compiler methods, see the <a href="#!/api/angular.compile"><code>Compiler API doc</code></a>.</p>
<h3>Related Topics</h3>
<ul>
<li><a href="#!/guide/dev_guide.compiler">Angular HTML Compiler</a></li>
<li><a href="#!/guide/dev_guide.compiler.directives">Angular Directives</a></li>
<li><a href="#!/guide/dev_guide.compiler.widgets">Angular Widgets</a></li>
<li><a href="#!/guide/dev_guide.compiler.directives.creating_directives">Creating Custom Directives</a></li>
</ul>
<h3>Related API</h3>
<ul>
<li><a href="#!/api/angular.compile"><code>Compiler API</code></a></li>
</ul></div>