angular.js/src/directives.js

112 lines
2.7 KiB
JavaScript
Raw Normal View History

2010-03-22 20:58:04 +00:00
angularDirective("ng-init", function(expression){
2010-03-16 17:30:26 +00:00
return function(){
2010-03-22 20:58:04 +00:00
this.$eval(expression);
2010-03-16 17:30:26 +00:00
};
});
2010-03-22 20:58:04 +00:00
angularDirective("ng-eval", function(expression){
2010-03-16 17:30:26 +00:00
return function(){
2010-03-22 20:58:04 +00:00
this.$addEval(expression);
};
2010-03-16 17:30:26 +00:00
});
2010-03-22 20:58:04 +00:00
angular.directive("ng-bind", function(expression){
return function(element) {
2010-03-16 17:30:26 +00:00
this.$watch(expression, function(value){
2010-03-22 20:58:04 +00:00
element.text(value);
2010-03-16 17:30:26 +00:00
});
};
});
2010-03-22 20:58:04 +00:00
angular.directive("ng-bind-attr", function(expression){
return function(element){
this.$watch(expression, bind(element, element.attr));
2010-03-16 17:30:26 +00:00
};
});
2010-03-22 20:58:04 +00:00
angular.directive("ng-non-bindable", function(){
this.descend(false);
});
angular.directive("ng-repeat", function(expression, element){
var reference = this.reference("ng-repeat: " + expression),
r = element.removeAttr('ng-repeat'),
template = this.compile(element),
path = expression.split(' in '),
lhs = path[0],
rhs = path[1];
var parent = element.parent();
element.replaceWith(reference);
2010-03-16 17:30:26 +00:00
return function(){
2010-03-22 20:58:04 +00:00
var children = [],
currentScope = this;
this.$addEval(rhs, function(items){
var index = 0, childCount = children.length, childScope, lastElement = reference;
foreach(items, function(value, key){
if (index < childCount) {
// reuse existing child
childScope = children[index];
} else {
// grow children
childScope = template(element.clone(), currentScope);
childScope.init();
childScope.scope.set('$index', index);
childScope.element.attr('ng-index', index);
lastElement.after(childScope.element);
children.push(childScope);
}
childScope.scope.set(lhs, value);
childScope.scope.updateView();
lastElement = childScope.element;
index ++;
2010-03-16 17:30:26 +00:00
});
2010-03-22 20:58:04 +00:00
// shrink children
while(children.length > index) {
children.pop().element.remove();
}
2010-03-16 17:30:26 +00:00
});
};
2010-03-22 20:58:04 +00:00
}, {exclusive: true});
/////////////////////////////////////////
/////////////////////////////////////////
/////////////////////////////////////////
/////////////////////////////////////////
/////////////////////////////////////////
2010-03-16 17:30:26 +00:00
//Styling
//
//ng-class
//ng-class-odd, ng-class-even
//ng-style
//ng-show, ng-hide
angular.directive("action", function(expression, element){
return function(){
var self = this;
jQuery(element).click(function(){
self.$eval(expression);
});
};
});
//ng-watch
// <div ng-watch="$anchor.book: book=Book.get();"/>
angular.directive("watch", function(expression, element){
var watches = {
'lhs':'rhs'
}; // parse
return function(){
this.$watch(watches);
};
2010-03-16 17:30:26 +00:00
});
//widget related
//ng-validate, ng-required, ng-formatter
//ng-error