2014-07-11 56 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/ –

相關問題