javascript - 在另一个 ng-repeat 中使用 ng-repeat

标签 javascript angularjs angularjs-ng-repeat

我正在开发一个页面,我需要在其中显示一些框(使用 ng-repeat ),其中包含 channel 信息以及显示位置(城市)。

我面临的问题是当我重复第二个 ng-repeat :

<table class="table table-condensed" ng-init="nitsCh = [objsCh[$index].nit]">

这应该获得第一个 ng-repeat 的 $index 并创建一个新数组,其中包含将显示 channel 的位置。它正是这样做的。但是,当我使用这个数组应用第二个 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>

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 节点将是动态的。

提前致谢!

最佳答案

ng-repeat的创建自己的$scope .

因此,对于内部 ng-repeats,您可以访问 $parent.$index , 其中 $parent是当前重复 block 的父范围。

例如:

<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>

Plunkr http://plnkr.co/edit/hOfAldNevxKzZQlxFfBn?p=preview

(从这个答案简化 passing 2 $index values within nested ng-repeat )

关于javascript - 在另一个 ng-repeat 中使用 ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31484686/

相关文章:

javascript - Morris.js 显示不正确

javascript - Angular 6 : unable to make http request

javascript - 能够使用 Controller 作为语法访问 `$scope` 和 `this`。为什么?

javascript - 如何使项目默认处于事件状态并在单击 Angular 中的其他项目后将其删除?

javascript - Angularjs ng-repeat 必须仅在单个 <li> 中显示

javascript - 为什么条形图表现得如此奇怪? html5 Canvas 和 javascript

javascript - Mocha JS 测试 - 一种对 sinon spy 进行回调的方法

javascript - 从指令在 Controller 中执行 javascript 函数

javascript - Angular ng-click 在 ng-repeat 中不起作用

javascript - ng-repeat 对象属性,但在输入后散焦输入框