2015-02-09 71 views
0

我已經叫下面的JavaScript對象 「標籤」:

http://i.imgur.com/hKTj9o9.png

我需要的頁面輸出:

section0.title 
     children0.title 
     children1.title 

其最終被

Analytical Balances 
     ML 
     XS 

我已經有選項卡名稱(tab0.title,tab1.title)重複爲標籤導航,並且想知道我可能會重複節名稱(例如section0.title)和子節名稱(例如children0.title)。

這是我的想法有可能實現這一結果:

  <div ng-repeat="tab in tabs" track by $index> 
       <a>{{ tab[$index].title }}</a> 
        <ul ng-repeat="section in tabs" track by $index> 
         <li>{{ section[$index].title }}</li> 
         <ul ng-repeat="child in children" track by $index> 
          <li>{{ child[$index].title }}</li> 
         </ul> 
        </ul> 
      </div> 

據我瞭解,這也將重複分頁標題,但我想它會更容易理解,如果我包括頂部水平。然後,我可以修改它,並根據索引每頁顯示一個標籤。

是否像這樣使用$索引來引用javascript變量可能?我現在意識到這可能不是一件事情。

非常感謝您的時間。

編輯:真棒謝謝你的輸入,你幾乎立即解決了我的問題。我現在看到爲什麼我使用$ index,並且可以使用更多的幫助。我現在有在我的顯示選項卡名稱此代碼:

<nav> 
     <ul> 
       <li ng-class="getClass($index)" ng-repeat="tab in tabArray"> 
        <a ng-click="toggleSelect($index)" ng-class="getLinkClass($index)">{{tab}}</a> 
       </li> 
     </ul> 
</nav> 

這不是使用「標籤」的對象,我介紹你,而是「tabArray」這僅僅是一個陣列上的頂級標籤名字順序。 toggleSelect使用$ index來允許我根據選中的選項卡更改顯示哪些內容,並選擇哪個選項卡獲取活動類。

現在回到爲什麼我認爲我需要$索引時,我發佈此問題。我的「標籤」對象中的選項卡被命名爲「tab0」,「tab1」等。當$ index爲0時,可以使用$ index重複僅來自tab0的元素嗎?這就是爲什麼我認爲我可以利用像

{{tabs["tab"+$index].title}} 

什麼的。甚至可以將我的選項卡命名爲「0」,「1」等,並且像

{{tabs[$index].title}} 

在我的HTML中。

非常感謝,請讓我知道如果我不清楚。

也可對照,我的簡單toggleSelect方法:

$scope.toggleSelect = function(ind){ 
      $scope.selectedIndex = ind; 


      } 

回答

1

你需要根據你的迭代要修改的項目,因爲每個那些相對於你已經迭代的項目。

總之,你有什麼是公正的3-每個循環:

<div ng-repeat="tab in tabs"> 
     <a>{{ tab.title }}</a> 
     <ul ng-repeat="section in tab.sections"> 
     <li>{{ section.title }}</li> 
     <ul ng-repeat="child in section.children"> 
      <li>{{ child.title }}</li> 
     </ul> 
     </ul> 
</div> 

編輯:要回答你的編輯,你可以改變頂線在上面的HTML是:

<div ng-repeat="tab in tabs" ng-if="tabIsActive($index)"> 

而在你的控制器有:

$scope.tabIsActive = function (index) { 
    //according to your edit, $scope.selectedIndex should hold the active tab index 
    return $scope.selectedIndex == index; 
} 
+0

非常感謝你,這立即奏效,但我做了一個編輯,請求我的下一步幫助。如果你有機會,請看看。 – user95227 2015-02-09 15:11:43

+0

@ user95227看我的編輯。 – 2015-02-09 15:19:10

+0

再次感謝所有的幫助,它帶來了巨大的變化。我剛剛爲這個相同的應用程序發佈了一個新問題,您可能會知道如何處理。請看看你是否有時間: http://stackoverflow.com/questions/28418412/having-trouble-repeating-correct-items-with-nested-ng-repeat-and-ng-if – user95227 2015-02-09 20:13:01

0

你不真正需要使用[$指數]字段,你就應該能夠做到這一點:

tab.title

與變革「一節中標籤」在標籤

爲「節.sections'

,然後做

section.title

ng-repeat已經爲您提供了tabs數組中的單個元素,該變量稱爲「tab」。部分同樣適用。

+0

你絕對正確。我做了一個編輯,解釋爲什麼我認爲我需要使用$ index。如果可以,請幫助我。非常感謝你的初步快速反應。 – user95227 2015-02-09 15:12:45

相關問題