2013-02-18 18 views
0

我是AngularJS的新手,但我非常享受迄今爲止的工作。我用AngularJS的NG-重複的HTML,其中一個節點的孩子放在節點的標記,這樣創建的樹結構:顯示爲列表的層次結構ng-repeat

<div class="parent"> 
    I am the parent node. 
    <div class="child"> 
     I am a child node. 
     <div class="child"> 
      I am a grandchild. 
      etc... 
     </div> 
    </div> 
</div> 

但想什麼,我做的反而是使用相同的數據(樹)來做這樣的標記:

<div class="parent">I am the parent</div> 
<div class="child">I am a child</div> 
<div class="child">I am a grandchild</div> 
<div class="child">I am a great-grandchild</div> 
etc.... 

即,列表。我想出了這樣做的唯一途徑是通過我叫visibleDescendants容器對象,這使得一個數組,看起來很像這創造了一種新方法:

this.visibleDescendants = function(a) { 
    if (!a) a = []; 
    if (this.selected()) { 
    a.push(this); 
    if (this.hasChildren()) { 
     this.children().forEach(function(child) { 
     if (child.selected()) { 
      a = child.visibleDescendants(a); 
     } 
     }); 
    } 
    } 
    return a; 
}; 

,然後設置控制器/模板像這樣:

$scope.visibleDescendants = $scope.container.visibleDescendants(); 
<div ng-repeat="column in visibleDescendants">...</div> 

,然後其中一個節點設置爲selected(false)每一次,我重置$ scope.visibleDescendants。

與其他我一直在閱讀/使用AngularJS的東西相比,這看起來效率不高,也不是非常習慣。我希望能夠找到一種方法在模板部分中更加聲明性地做到這一點。請幫忙!

+0

不久前我回答了simillar問題:http://stackoverflow.com/questions/14921992/angularjs-ng-repeat-on-two-levels-but-just-one-output/14922966#14922966 hope這有助於。 – 2013-02-18 20:07:31

回答

0

我認爲我真正想要做的是一個介紹的問題,不應該影響標記。換句話說,數據和標記應該在結構上/語義上匹配;只有CSS應該改變它的外觀。

這就是說,我不認爲這是明智的混亂重建數據用於演示目的。