我将从Firebase返回的结果作为具有层次结构的数组返回如下:
[
[
{name: "a", id: 1},
{name: "b", id: 2},
{name: "c", id: 3}
],
[
{name: "d", id: 1},
{name: "e", id: 2}
]
]
如何列出该层次结构中用于ng-repeat的每个名称?
我可以使用$ asArray()列出每个数组的名称,但我无法在所有数组中检索它们。我可以将每个数组的索引作为变量吗?
这是我的重复:
<ul ng-repeat="cha in character">
<li href="/link/{{each_array_index(1,2,3...)}}/{{cha.id}}">{{cha.name}}</li>
</ul>
答案 0 :(得分:1)
你需要有两个嵌套的中继器,在你的表达式'cha in character'中,cha包含一个对象数组;你应该有像
这样的东西ng-repeat ='列表'
中的子列表 子列表中的ng-repeat ='字符'
或者你可以压扁你正在接收的阵列响应
var xs = [
[
{name: "a", id: 1},
{name: "b", id: 2},
{name: "c", id: 3}
],
[
{name: "d", id: 4},
{name: "e", id: 5}
]
];
var merged = [];
merged = merged.concat.apply(merged, xs);
或者您可以创建过滤器
angular.module('myApp', [])
.controller('HomeCtrl', function($scope){
$scope.xs = [
[
{name: "a", id: 1},
{name: "b", id: 2},
{name: "c", id: 3}
],
[
{name: "d", id: 4},
{name: "e", id: 5}
]
];
})
.filter('flatten', function(){
return function(xs) {
var merged = [];
return merged.concat.apply(merged, xs);
}
})
;
<body ng-app="myApp" ng-controller="HomeCtrl">
<h1>Hello Plunker!</h1>
<ul ng-repeat="cha in xs | flatten">
<li><a href="/link/{{$index}}/{{cha.id}}">{{cha.name}} - {{cha.id}}</a></li>
</ul>
</body>
嵌套列表解决方案 在评论中描述
<ul ng-repeat="list in xs">
<li ng-repeat="cha in list">
<a href="/link/{{$parent.$index}}/{{cha.id}}">{{cha.name}}</a>
</li>
</ul>