在嵌套的ng-repeat中传递2 $ index值

Tul*_*les 316 angularjs angularjs-ng-repeat

所以我有一个ng-repeat嵌套在另一个ng-repeat中,以便构建一个导航菜单.在<li>内部ng-repeat循环中的每一个上,我设置了一个ng-click,它通过传入$ index来调用该菜单项的相关控制器,让应用程序知道我们需要哪一个.但是我还需要从外部ng-repeat传入$ index,以便app知道我们所在的部分以及哪个教程.

<ul ng-repeat="section in sections">
    <li  class="section_title {{section.active}}" >
        {{section.name}}
    </li>
    <ul>
        <li class="tutorial_title {{tutorial.active}}" ng-click="loadFromMenu($index)" ng-repeat="tutorial in section.tutorials">
            {{tutorial.name}}
        </li>
    </ul>
</ul>
Run Code Online (Sandbox Code Playgroud)

这是一个Plunker http://plnkr.co/edit/bJUhI9oGEQIql9tahIJN?p=preview

Ale*_*orn 461

每个ng-repeat使用传递的数据创建子范围,并$index在该范围中添加另一个变量.

所以你需要做的是达到父范围,然后使用它$index.

http://plnkr.co/edit/FvVhirpoOF8TYnIVygE6?p=preview

<li class="tutorial_title {{tutorial.active}}" ng-click="loadFromMenu($parent.$index)" ng-repeat="tutorial in section.tutorials">
    {{tutorial.name}}
</li>
Run Code Online (Sandbox Code Playgroud)

  • 您不必使用数组:`ng-click ="loadFromMenu($ parent.$ index,$ index)"` (16认同)
  • 你不应该使用$ parent.$ index,因为它不是很安全.如果你在循环中添加一个ng-if,你会得到$ index混乱,请检查:http://plnkr.co/52oIhLfeXXI9ZAynTuAJ (7认同)
  • 你甚至不必传入索引 - 在loadFromMenu中,它应该有权访问这个对象,这将使你能够访问:this.$ index和this.$ parent.$ index (3认同)
  • @Oddman即使这是可能的,我也不认为这是一个好主意,因为你然后硬连接你的loadFromMenu在一个对象的上下文中运行,该对象具有$ index和带有$ index的父作用域.例如,假设您在菜单中创建了在两个重要区域之间生成另一​​个范围的组 - 您将不得不更改loadFromMenu而不是更改对它的调用.如果在某些菜单中你需要调用`loadFromMenu($ parent.$ parent.$ index,$ index)`而在某些你需要`loadFromMenu($ parent.$ index,$ index)`然后使用`this .. ..`将无法正常工作. (3认同)

vuc*_*lur 201

$parent.$index使用方式更优雅的解决方案ng-init:

<ul ng-repeat="section in sections" ng-init="sectionIndex = $index">
    <li  class="section_title {{section.active}}" >
        {{section.name}}
    </li>
    <ul>
        <li class="tutorial_title {{tutorial.active}}" ng-click="loadFromMenu(sectionIndex)" ng-repeat="tutorial in section.tutorials">
            {{tutorial.name}}
        </li>
    </ul>
</ul>
Run Code Online (Sandbox Code Playgroud)

Plunker:http://plnkr.co/edit/knwGEnOsAWLhLieKVItS?p = info

  • 当您从列表中删除项目时,这将停止工作,因为ng-init值不会重新初始化.仅当您不打算从列表中删除项目时,它才有用. (42认同)
  • 看起来像角度语法演变.http://stackoverflow.com/a/31477492/2516560您现在可以在数据中使用"ng-repeat =(键,值)" (6认同)
  • 这是实现此效果的推荐方法.实际上,有角度的团队曾多次表示这是ng-init指令的少数推荐用法之一(参见:[link](https://docs.angularjs.org/api/ng/directive/ngInit) ).我经常发现使用$ parent(根据当前接受的答案)有点代码味道,因为通常有更好的方法来实现$ scope范围通信(服务或广播/发送事件)并将其分配给a命名变量使值变得更清晰. (2认同)
  • IMO这个答案比接受的要好.使用$ parent实际上是非常缺乏的. (2认同)
  • 在那些日子里,我写了这个答案,这是为Angular 1.X做的.`ng-init`的使用在`ng-init`文档中进行了演示,在ng-repeat`文档中没有提及,所以我在这里写了+修改了Github上的文档.当前的"ng-repeat"语法有更好的方法来实现这一点,@ Okazari证明了这一点.它没有你在评论中提到的一些不完善之处. (2认同)

Oka*_*ari 108

使用这种语法怎么样(看看这个plunker).我刚刚发现了它,它非常棒.

ng-repeat="(key,value) in data"
Run Code Online (Sandbox Code Playgroud)

例:

<div ng-repeat="(indexX,object) in data">
    <div ng-repeat="(indexY,value) in object">
       {{indexX}} - {{indexY}} - {{value}}
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

使用此语法,您可以为自己的名称指定$index并区分这两个索引.

  • 我认为当你有多个嵌套循环时,这比使用父更清晰 (8认同)
  • 这是完成此任务并避免任何潜在问题的正确方法.ng-init适用于初始渲染,但如果对象在页面上更新,则会中断. (3认同)

gon*_*lon 32

只是为了帮助那些到达这里的人......你不应该使用$ parent.$ index,因为它不是很安全.如果你在循环中添加一个ng-if,你会得到$ index混乱!

正确的方法

  <table>
    <tr ng-repeat="row in rows track by $index" ng-init="rowIndex = $index">
        <td ng-repeat="column in columns track by $index" ng-init="columnIndex = $index">

          <b ng-if="rowIndex == columnIndex">[{{rowIndex}} - {{columnIndex}}]</b>
          <small ng-if="rowIndex != columnIndex">[{{rowIndex}} - {{columnIndex}}]</small>

        </td>
    </tr>
  </table>
Run Code Online (Sandbox Code Playgroud)

检查:plnkr.co/52oIhLfeXXI9ZAynTuAJ