ng-repeat 目前没有可能的方法来复杂地迭代对象内部(在 python 中可能的方式)。查看 ng-repeat source code 并注意匹配的正则表达式为:
(key, value) in collection - 他们推入键数组并分配给值列表,所以你不可能有一个复杂的 ng-repeat 遗憾...
这里基本上已经回答了 2 种类型的解决方案:
- 嵌套 ng-repeat 就像建议的第一个答案一样。
- 按照建议的第二个答案重新构建您的数据对象以适应 1 ng-repeat。
我认为解决方案 2 更好,因为我喜欢将排序和编码逻辑保留在控制器中,而不是在 HTML 文档中处理它。这也将允许更复杂的排序(即基于价格、金额、widgetName 或其他一些逻辑)。
另一件事 - 第二种解决方案将迭代数据集的可能方法(因为那里没有使用 hasOwnProperty)。
我已经改进了这个 Plunker 中的解决方案(基于finishmove Plunker),以便使用 angular.forEach 并表明该解决方案相当简单但允许复杂的排序逻辑.
$scope.buildData = function() {
var returnArr = [];
angular.forEach($scope.data, function(productData, widget) {
angular.forEach(productData, function( amount, price) {
returnArr.push( {widgetName: widget, price:price, amount:amount});
});
});
//apply sorting logic here
return returnArr;
};
$scope.sortedData = $scope.buildData();
然后在你的控制器中:
<div ng-controller="MyCtrl">
<table>
<thead>
<tr>
<td>thing</td>
<td>price</td>
<td>amount</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in sortedData">
<td>{{ item.widgetName }}</td>
<td>{{ item.price|currency }}</td>
<td>{{ item.amount }} </td>
</tr>
</tbody>
</table>
</div>