【问题标题】:using ng-repeat inside another ng-repeat在另一个 ng-repeat 中使用 ng-repeat
【发布时间】: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 &amp;&amp; indexO&lt;=10"

标签: javascript angularjs angularjs-ng-repeat


【解决方案1】:

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>

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

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

【讨论】:

  • 没用。我试图在这里插入它: 但如果我这样做的话作品:
  • 也许 plunkr 会有所帮助。我很难处理你的变量名,比如nitsCh,objsCh,indexN,nitCh
  • 我冒昧为您这样做,请更新 plunk:plnkr.co/edit/9IAcPfeZRUJQLF66xVPw?p=preview
  • 如果这仍然不能回答您的问题,您需要解释您要对 ng-init 块做什么,因为您的语言非常神秘。
  • loadFromMenu 应该真正将父对象作为参数传递以使其更简单
猜你喜欢
  • 1970-01-01
  • 2017-01-28
  • 1970-01-01
  • 2020-07-28
  • 1970-01-01
  • 2017-12-22
  • 2013-11-23
  • 1970-01-01
  • 2017-07-24
相关资源
最近更新 更多