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

时间:2021-07-14 07:41:18

In my controller , i have the data like...

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

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

$scope.docStructure = data

Is there any way that i can user ng-repeat to iterate over the keys and again iterate if the value is an array?

是否有任何方法可以使用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>

The ideas is I wanted to do a cross tab as below.

想法是我想做一个如下所示的交叉表。

Example:

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

1 个解决方案

#1


2  

You just need to add track by $index to the ng-repeat on the td tag

您只需要通过$ index将轨道添加到td标记上的ng-repeat

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

Here's a demo

这是一个演示

You could also convert your "features" arrays to contain objects instead of primitives, and then you don't need the track by $index

您还可以将“特征”数组转换为包含对象而不是基元,然后您不需要$ index跟踪

Here's a demo of that.

这是一个演示。

#1


2  

You just need to add track by $index to the ng-repeat on the td tag

您只需要通过$ index将轨道添加到td标记上的ng-repeat

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

Here's a demo

这是一个演示

You could also convert your "features" arrays to contain objects instead of primitives, and then you don't need the track by $index

您还可以将“特征”数组转换为包含对象而不是基元,然后您不需要$ index跟踪

Here's a demo of that.

这是一个演示。