Bru*_*las 18 javascript angularjs angularjs-ng-repeat
我正在开发一个页面,我需要显示一些ng-repeat包含频道信息的框(使用)以及它将在何处显示(哪些是城市).
我面临的问题是当我重复第二个问题时ng-repeat:
<table class="table table-condensed" ng-init="nitsCh = [objsCh[$index].nit]">
Run Code Online (Sandbox Code Playgroud)
这应该得到第一个ng-repeat的$ index并创建一个新的数组,其中显示了通道的位置.它确实如此.但是,当我使用此数组应用第二个ng-repeat时,它无法正常工作.
说我的html看起来像这样(PS:我需要使用索引值而不是objCh.name因为我将这些框放入列中)
<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>
Run Code Online (Sandbox Code Playgroud)
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;
});
});
Run Code Online (Sandbox Code Playgroud)
而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"
}]
}]
}
Run Code Online (Sandbox Code Playgroud)
当我尝试这个它工作:
<table class="table table-condensed" ng-init="nitsCh = [objsCh[($parent.$index + 1)].nit]">
Run Code Online (Sandbox Code Playgroud)
但我不能这样做,因为json节点将是动态的.
提前致谢!
jak*_*ker 36
ng-repeat创造属于自己的$scope.
因此,对于内部ng-repeats,您可以访问$parent.$index,其中$parent是当前重复块的父作用域.
例如:
<ul ng-repeat="section in sections">
<li>
{{section.name}}
</li>
<ul>
<li ng-click="loadFromMenu($parent.$index)" ng-repeat="tutorial in section.tutorials">
{{tutorial.name}}
</li>
</ul>
</ul>
Run Code Online (Sandbox Code Playgroud)
Plunkr http://plnkr.co/edit/hOfAldNevxKzZQlxFfBn?p=preview
(从这个答案中简化,在嵌套的ng-repeat中传递2 $索引值)
| 归档时间: |
|
| 查看次数: |
49367 次 |
| 最近记录: |