2010-11-22 20:05:01 +00:00
|
|
|
@workInProgress
|
|
|
|
|
@ngdoc overview
|
|
|
|
|
@name angular.widget
|
|
|
|
|
@namespace Namespace for all widgets.
|
|
|
|
|
@description
|
|
|
|
|
# Overview
|
2011-01-19 23:42:11 +00:00
|
|
|
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
|
2010-11-22 20:05:01 +00:00
|
|
|
adding new elements whereas directives are expected to only modify
|
|
|
|
|
element properties.
|
|
|
|
|
|
|
|
|
|
Widgets come in two flavors: element and attribute.
|
|
|
|
|
|
|
|
|
|
# Element Widget
|
2011-01-19 23:42:11 +00:00
|
|
|
Let's say we would like to create a new element type in the
|
|
|
|
|
namespace `my` that can watch an expression and alert() the user
|
2010-11-22 20:05:01 +00:00
|
|
|
with each new value.
|
|
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
|
<my:watch exp="name"/>
|
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
|
|
You can implement `my:watch` like this:
|
|
|
|
|
<pre>
|
|
|
|
|
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);
|
2011-05-13 14:15:59 +00:00
|
|
|
});
|
2010-11-22 20:05:01 +00:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
|
|
# Attribute Widget
|
2011-01-19 23:42:11 +00:00
|
|
|
Let's implement the same widget, but this time as an attribute
|
2010-11-22 20:05:01 +00:00
|
|
|
that can be added to any existing DOM element.
|
|
|
|
|
<pre>
|
2011-04-10 21:16:08 +00:00
|
|
|
<div my:watch="name">text</div>
|
2010-11-22 20:05:01 +00:00
|
|
|
</pre>
|
|
|
|
|
You can implement `my:watch` attribute like this:
|
|
|
|
|
<pre>
|
|
|
|
|
angular.widget('@my:watch', function(expression, compileElement) {
|
|
|
|
|
var compiler = this;
|
|
|
|
|
return function(linkElement) {
|
|
|
|
|
var currentScope = this;
|
|
|
|
|
currentScope.$watch(expression, function(value){
|
|
|
|
|
alert(value);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
|
|
@example
|
2011-02-01 00:21:29 +00:00
|
|
|
<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>
|