首页 文章

当动态键值是数组时,如何使用ng-repeat

提问于
浏览
1

在我的控制器中,我有像......这样的数据

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

$scope.docStructure = data

是否有任何方法可以使用ng-repeat来迭代密钥,如果值是数组则再次迭代?

<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

1 回答

  • 2

    您只需要将 track by $index 添加到td标记上的ng-repeat

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

    这是demo

    您还可以将"features"数组转换为包含对象而不是基元,然后您不需要 track by $index

    这是demo .

相关问题