2014-05-22 37 views
1

在我的控制,我有這樣的數據...如何使用NG-重複時的動態密鑰值是一個數組

data = { 
     chartTypes":["pie","donut","bar","line"], 
     "features":{ 
      "stacked": ["true","true","true","false"], 
      "percentage":["false","false","true","false"] 
     } 
    }; 

$scope.docStructure = data 

有什麼辦法,我可以用戶NG-重複迭代在鍵上並且如果值是數組,則再次迭代?

<div ng-app="chartFeatures" ng-controller="chartFeaturesCtrl" style="height:200px; background:red;"> 
     <table width="100%" border="0" cellpadding="0" cellspacing="0"> 
     <tr> 
      <th>&nbsp;</th> 
      <th ng-repeat="chartType in docStructure.chartTypes">{{chartType}}</th> 
     </tr> 
     <tr ng-repeat="(feature,supportedList) in docStructure.features"> 
      <td>{{feature}}</td> 
      <td ng-repeat="supported in supportedList">{{supported}}</td> 
     </tr> 

     </table> 
    </div> 

想法是我想做一個交叉表如下。

例子:

enter image description here

+0

什麼是錯誤? –

+0

錯誤:ngRepeat:dupes。 Repeater中的重複鍵 – shibualexis

+1

不要重複原始類型!這是不好的做法。如果你必須,所有ng重複加上'track by $ index' – Mike

回答

2

你只需要添加track by $index到NG-重複的td標籤

<td ng-repeat="supported in supportedList track by $index">{{supported}}</td> 

這裏有一個demo

你也可以轉換你的「功能「數組來包含對象而不是基元,然後你不需要track by $index

這是demo

+0

這工作Jerrad。非常感謝。 – shibualexis

相關問題