- 如果
show-length
指令设置了 transclude
属性,则 div
的节点内容被重新编译,得到的 link
函数作为指令 compile
函数的参数传入。
- 不管是上面的哪种情况,都会继续处理到
{{ text }}
这段文本。
- app.directive('showLength', function($rootScope, $document){
var func = function(element, attrs, link){
return function(scope, ielement, iattrs, controller){
var node = link(scope);
ielement.append(node);
var lnode = $('<span></span>');
ielement.prepend(lnode);
scope.$watch(function(scope){
lnode.text(node.text().length);
});
};
}
return {compile: func,
transclude: true, // element是节点没,其它值是节点的内容没
restrict: 'A'};
});