forked from angular/code.angularjs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular.widget.html
70 lines (64 loc) · 2.44 KB
/
angular.widget.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
<h1>angular.widget</h1>
<fieldset class="workInProgress">
<legend>Work In Progress</legend>
This page is currently being revised. It might be incomplete or contain inaccuracies.
</fieldset>
<h2>Overview</h2>
<p>Widgets allow you to create DOM elements that the browser doesn't
already understand. You create the widget in your namespace and
assign it behavior. You can only bind one widget per DOM element
(unlike directives, in which you can use any number per DOM
element). Widgets are expected to manipulate the DOM tree by
adding new elements whereas directives are expected to only modify
element properties.</p>
<p>Widgets come in two flavors: element and attribute.</p>
<h2>Element Widget</h2>
<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 alert() the user
with each new value.</p><div ng:non-bindable><pre class="brush: js; html-script: true;">
<my:watch exp="name"/>
</pre></div><p>You can implement <code>my:watch</code> like this:</p><div ng:non-bindable><pre class="brush: js; html-script: true;">
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>Attribute Widget</h2>
<p>Let's implement the same 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;">
<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; html-script: true;">
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>Example</h2>
<doc:example>
<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>
</doc:source>
<doc:scenario></doc:scenario>
</doc:example>