2014-03-26 50 views
3

有兩種方法來創建在角角遞歸

遞歸的第一個使用$編譯功能和手動編寫內容


鏈路:Angularjs: understanding a recursive directive

compile: function(tElement, tAttr) { 
     var contents = tElement.contents().remove(); 
     var compiledContents; 
     return function(scope, iElement, iAttr) { 
      if(!compiledContents) { 
       compiledContents = $compile(contents); 
      } 
      compiledContents(scope, function(clone, scope) { 
        iElement.append(clone); 
      }); 
     }; 
    }, 

示例:http://jsfiddle.net/n8dPm/


第二個採用NG-包括

鏈接:https://groups.google.com/forum/?fromgroups=#!topic/angular/TbpjE-5XEM0

<script type="text/ng-template" id="tree_item_renderer.html"> 
{{data.name}} 
<button ng-click="add(data)">Add node</button> 
<button ng-click="delete(data)" ng-show="data.nodes.length > 0">Delete nodes</button> 
<ul> 
    <li ng-repeat="data in data.nodes" ng-include="'tree_item_renderer.html'"></li> 
</ul> 

例如:http://jsfiddle.net/brendanowen/uXbn6/8/


我的問題是:什麼是利弊兩其中 和是否有可能使用包含一個自定義ng-include指令?

回答

0

我相信你錯過了兩個有價值的選擇。在指令的模板