2015-02-09 45 views
0

我有以下的html:遇到問題重複使用嵌套NG-重複和正確的項目NG-如果

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

        <ul ng-repeat="section in tab.sections"> 

        <h2 ng-class="productTitle" ng-repeat="child in section.children" ng-if="sideTabIsActive(section, $index)"> 
         <img ng-src="{{ child.productImageURL }}"/> 
         <li>{{ child.title }}</li> 
        </ul> 
      </div> 

這是 「標籤」 JavaScript對象是什麼樣子:

Screenshot of tabs object in debugger

我想要的輸出是選擇該部分時爲每個部分列出的子類別標題(例如children0.title)和圖像(例如children0.productImageURL)。

實施例所需的輸出:

當分析天平被點擊

(ML image) //which is section0.child0.productImageURL 
ML   //which is section0.child0.title 

(XS image) //which is section0.children1.productImageURL 
XS   //which is section0.child1ren.title 

目前,我顯示該:

當分析天平點:

(ML image) //which is section0.children0.productImageURL 
ML   //which is section0.children0.title 

(XPE image) //which is section1.children0.productImageURL 
XPE   //which is section1.children0.title 

我怎樣才能列出每個部分的兩個孩子(和相關圖像)在哪一部分被選中(selectedSideIndex)?

除了上述HTML,這裏是相關的HTML和JavaScript,我在我試圖達到所需的輸出使用:

$scope.toggleSideSelect = function(ind){ 
      $scope.selectedSideIndex = ind; 

    } 


$scope.sideTabIsActive = function (tab, $index) { 
      var selectedIndexTest = $scope.selectedSideIndex 
      return $index==$scope.selectedSideIndex; 

}

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

        <ul ng-repeat="section in tab.sections" ng-click="toggleSideSelect($index)"> 
        <li>{{ section.title }}</li> 
        <ul ng-repeat="child in section.children"> 
         <li>{{ child.title }}</li> 
        </ul> 
        </ul> 
      </div> 
+0

JSON中全局對象的結構如何? – 2015-02-09 20:28:17

+0

@RichardCotrina它是從app.data文件夾中的文件動態創建的,所以我沒有創建對象的JavaScript代碼片段向您展示。我可以寫出一個,因爲它很大,所以只需要一點。以下是來自鉻檢查員的相關對象的更好的鏡頭:http://i.imgur.com/rcdoY4X.png 這有幫助嗎? – user95227 2015-02-09 20:35:24

+0

哦,那麼我建議你改變你的Dinamically生成的數據的結構,以適應你需要使用的東西和你需要展示的東西之間的平衡。 – 2015-02-09 21:04:07

回答

0

你幾乎有:)

你需要做的是這樣的:

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

     <ul ng-repeat="section in tab.sections" ng-init="sectionIndex = $index"> 

      <h2 ng-class="productTitle" ng-repeat="child in section.children" ng-if="sideTabIsActive(section, sectionIndex)"> 
       <img ng-src="{{ child.productImageURL }}"/> 
       <li>{{ child.title }}</li> 
     </ul> 
</div> 

這樣,使用ng-init="sectionIndex = $index"您可以保存該節的$index,並且可以在嵌套的ng-repeat上使用它。它也可以使用$parent.$index,但我不會推薦它,因爲結構可能會改變,你將留下一個錯誤。

這樣,您可以使用該部分的索引調用ng-if="sideTabIsActive(section, sectionIndex)",只有活動部分的孩子才能看到。

編輯:你應該的

$scope.sideTabIsActive = function (tab, $index)

簽名既然你不實際使用的功能tab改變

$scope.sideTabIsActive = function ($index)

。 (如果你這樣做,不要忘記從視圖中改變對該函數的調用)

+0

謝謝先生!到目前爲止,你已經幫助了我很多,但現在我試圖以類似的方式從對象中的數組中重複數據,並且我再次掙扎。我最近沒有花時間,真正感謝你的幫助。正如你現在知道這個項目,我想我會把你連接到我的新問題: http://stackoverflow.com/questions/28442303/how-to-repeat-elements-pulled-from-an-array- javascript-object-literal -w 讓我知道你是否有任何見解或者我不清楚。我保證我試圖先弄清楚自己的事情。這是我第一次使用Angular – user95227 2015-02-10 21:42:35

+0

嗨Omri,很抱歉在一段時間後會出現bug。但是這個應用程序正在使用這個動態創建的對象和'ng-repeat'。不過,我在設置初始數據時遇到了麻煩,只能在此處發佈一個問題: http://stackoverflow.com/questions/28768240/can-i-set-this-something-of-a-service -in-an-an-nested-object-that-is-dynamic 對不起,如果以這種方式伸出是不合適的。你之前給我提供的幫助比任何人都多,我想我會看看你是否有一些時間。 – user95227 2015-02-27 15:25:35