如何在对象数组中使用ng-repeat?

时间:2015-10-20 12:13:48

标签: javascript arrays angularjs multidimensional-array ng-repeat

你有什么想法吗?

我尝试使用ng-repeat制作一个数组数组,如下所示:

jsfiddle example

function MyCtrl($scope) {
    $scope.items = 
        [
         {'adam1': [{id:10, content:test1}, {id:11, content:test2},          
                    {id:12, content:test3}]}, 
         {'adam2': [{id:20, content:test4}, {id:21, content:test5},          
                    {id:30, content:test6}]}, 
         {'adam1': [{id:10, content:xxx}]}
         ];
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<div ng-app ng-controller="MyCtrl">
    <ul>
        <li ng-repeat="(key, value) in items">{{key}}: {{value.content}}</li>
    </ul>
</div>

2 个答案:

答案 0 :(得分:1)

一种方法( fiddle ):

<div ng-repeat="item in items">
    <ul ng-repeat="(key, value) in item">
        <li ng-repeat="obj in value">{{obj.content}}</li>
    </ul>
</div>

那就是说你可能想要重新排列结构,以避免嵌套ng-repeat

答案 1 :(得分:0)

您分配变量的方法运行正常,但可能不像您期望的那样:updated jsfiddle

content项目将无法使用,因为value是主数组中的每个对象;您需要遍历其属性(即adamX键)以获取嵌套数组。