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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31484686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:45:22  来源:igfitidea点击:

using ng-repeat inside another ng-repeat

javascriptangularjsangularjs-ng-repeat

提问by Bruno Ornelas

I'm developing a page where I need to show some boxes (using ng-repeat) that contains info of channels and where it will be shown (which are cities).

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

The problem I am facing is when I repeat the second ng-repeat:

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

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

This should get the $index of first ng-repeat and create a new array with the places the channels will be shown. And it does exactly that. But, when I apply the second ng-repeat using this array, it doesn't work properly.

这应该获得第一个 ng-repeat 的 $index 并创建一个新数组,其中包含将显示频道的位置。而它正是这样做的。但是,当我使用此数组应用第二个 ng-repeat 时,它无法正常工作。

Said that my html looks like this (PS: I need to use the index value instead of objCh.namebecause I place these boxes into columns)

说我的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>

The JS file looks like this:

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;
    });
});

And the json file (that php create) looks like this:

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"
        }]
    }]
}

When I try this it works:

当我尝试这个时它有效:

<table class="table table-condensed" ng-init="nitsCh = [objsCh[($parent.$index + 1)].nit]">

But I can't make it this way because the json nodes will be dynamic.

但我不能这样做,因为 json 节点将是动态的。

Thanks in advance!

提前致谢!

回答by jakeforaker

ng-repeat's create their own $scope.

ng-repeat的创建自己的$scope

So, for the inner ng-repeats, you have access to $parent.$index, where $parentis the parent scope of the current repeat block.

因此,对于内部NG-重复,您可以访问$parent.$index,在那里$parent是当前重复块的父范围。

For ex:

例如:

<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

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

(simplified from this answer passing 2 $index values within nested ng-repeat)

(从这个答案中简化,在嵌套的 ng-repeat 中传递 2 $index 值