【发布时间】:2015-10-07 16:45:06
【问题描述】:
我正在开发一个页面,我需要在其中显示一些框(使用ng-repeat),其中包含频道信息以及显示位置(城市)。
我面临的问题是当我重复第二个ng-repeat:
<table class="table table-condensed" ng-init="nitsCh = [objsCh[$index].nit]">
这应该获取第一个 ng-repeat 的 $index 并创建一个新数组,其中包含将显示通道的位置。它正是这样做的。但是,当我使用此数组应用第二个 ng-repeat 时,它无法正常工作。
说我的html是这样的(PS:我需要使用索引值而不是objCh.name因为我把这些box放到了列中)
<div class="box box-solid box-default" ng-repeat="(indexO, objCh) in objsCh track by indexO" ng-if="indexO%4==0 && indexO<=10">
<div class="box-header">
<div class="pull-left">
<img src="dist/img/channels/{{ objsCh[indexO].pic }}" data-toggle="tooltip" title="" data-original-title="Alterar logo do canal">
<h3 class="box-title">{{ objsCh[(indexO)].name }}</h3>
</div>
<div class="box-tools pull-right">
<button class="btn btn-box-tool" data-toggle="tooltip" title="" data-original-title="Adicionar ou Remover NIT"><i class="fa fa-plus"></i></button>
</div>
</div>
<div class="box-body">
<table class="table table-condensed" ng-init="nitsCh = [objsCh[indexO].nit]">
<tbody>
<tr>
<th style="width: 10px">#</th>
<th>Nit</th>
</tr>
<tr ng-repeat="(indexN,nitCh) in nitsCh track by indexN">
<td>{{ objsCh[(indexO + 1)].nit[indexN].num }}</td>
<td>{{ objsCh[(indexO + 1)].nit[indexN].name }}</td>
</tr>
</tbody>
</table>
</div>
</div>
JS 文件如下所示:
var app = angular.module('appApp', []);
app.controller('ctrlApp', function($scope, $http) {
var url = "includes/exibChNit.php";
$http.get(url).success(function(response) {
all = response;
$scope.objsCh = all.channels;
});
});
而 json 文件(由 php 创建)如下所示:
{
"channels": [{
"id": "1",
"sid": "1",
"name": "Channel",
"pic": "channel.jpg",
"crc32": "A1g423423B2",
"special": "0",
"url": "",
"key": "",
"type": "",
"city": [{
"num": "1",
"name": "S�o Paulo"
}, {
"num": "2",
"name": "Rio de Janeiro"
}]
}, {
"id": "2",
"sid": "2",
"name": "CHannel 1",
"pic": "channel.jpg",
"crc32": "A1F5534234B2",
"special": "0",
"url": "",
"key": "",
"type": "",
"city": [{
"num": "1",
"name": "S�o Paulo"
}, {
"num": "2",
"name": "Rio de Janeiro"
}]
}]
}
当我尝试这个时,它会起作用:
<table class="table table-condensed" ng-init="nitsCh = [objsCh[($parent.$index + 1)].nit]">
但我不能这样做,因为 json 节点将是动态的。
提前致谢!
【问题讨论】:
-
为您减 1:用于添加包含我对
$parent.$index的回答但未提及的编辑。 -
这太复杂了。解释由于列而无法使用对象本身是什么意思。我已经做了很多深度 ng-repeat 嵌套,而没有您使用的所有额外索引
-
也可以使用
limitTo过滤器代替ng-if="indexO%4==0 && indexO<=10"
标签: javascript angularjs angularjs-ng-repeat