2014-07-11 58 views
0

比方说,我有这个在我的HTML:AngularJS指令使元素加倍?

<div my-doubling-directive> 
    ... Lots of children here ... 
</div> 

我的指令运行后我想

... Lots of children here ... 
... Lots of children here ... 

所以基本上翻倍的输出,而不是离开的div指令是。我想我需要某种类型的链接方法transclude,但我卡住了。我已经看到了另一个通过过滤器实现这个功能的例子,但我现在将它作为自定义指令的学习示例。

+0

使用指令transclusion可能是我需要借此下一步,使其动态多少次,最简单的方法 – pixelbits

回答

1

您可以使用两次ng-transclude

app.directive("myDoublingDirective", function() { 
    return { 
     transclude: true, 
     template: "<div ng-transclude></div><div ng-transclude></div>", 
    }; 
}); 

http://jsfiddle.net/agtGt/1/

+0

,所以我结束了这一点。是我能找到使模板变得动态的唯一方法。 .directive( 'elementRepeat',函数(){ 返回{ 链路:功能(范围,元件,ATTRS){ VAR HTML = element.html(); element.empty(); 为(VAR I = parseInt(attrs.elementrepeat,10) - 1; i> = 0; i--){ element.append(html); } } }; – Scott

+0

@Scott你可以在ng中使用ng-repeat吗? -transclude'? –

+0

@Scott http://jsfiddle.net/agtGt/2/ –

相关问题