嵌套的ng-repeat $ index无法正常工作

时间:2016-04-30 13:12:45

标签: angularjs angularjs-ng-repeat

这里我想制作一个页面编号,每次ng-repeat增量时都会增加,但我得到的是第二个ng-repeat索引始终为零,但使用第一个ng-repeat的索引可以正常通过$ parent工作。 $ index如何解决这个问题? 这是代码示例:

<div ng-repeat="chapter in plan.chapters" class="export-page" >
                      <div  ng-repeat="section in chapter.sections track by section.id" class="export-page" ng-init="sectionindex = $index">

                          <div class="header">
                              <br>
                              <h2 class="headerTitle">
                                <b>{{$root.selectedCompany.name + ' | ' + chapter.subject}}</b>
                              </h2>
                          </div>

                          <div class="content">
                              <h1><b>{{chapter.subject}}</b></h1>
                              <h2 class="chapter" >{{section.subject}}</h2>

                              <br>

                              <p ng-if="section.content_type === 'text'">
                                <div ng-bind-html="section.content.text"></div>
                              </p>

                              <p ng-if="section.content_type === 'table'">
                                <numeric-table model="section.content"
                                              disable-editing="true"
                                              hide-input-controls="true">
                                </numeric-table>
                              </p>

                              <p ng-if="section.content_type === 'aggregated-table'">
                                <aggregated-table model="section.selectedTables">
                                </aggregated-table>
                              </p>

                              <p ng-if="section.content_type === 'text-table'">
                                <free-text-table model="section.content"
                                                disable-editing="true"
                                                hide-input-controls="true">
                                </free-text-table>
                              </p>

                              <p ng-if="section.content_type === 'chart'">
                                <multi-chart model="section.content"
                                            tables="section.selectedTables"
                                >
                                </numeric-table>
                              </p>
                          </div>
                          <div class="footer col-xs-12">
                              <div class="txt col-xs-11">copyright 2016@ {{$root.selectedCompany.name}}</div>
                              <div class="pp col-xs-1 ">Page{{sectionindex}} </div>
                          </div>
                      </div>
                  </div>

1 个答案:

答案 0 :(得分:0)

<div ng-repeat="(index1 ,chapter) in plan.chapters" class="export-page" >
   <div  ng-repeat="(index2,section) in chapter.sections track by section.id" class="export-page" ng-init="sectionindex = $index">
        Outer Repeat Index{{index1}}
        Inner Repeat Index{{index2}}
   </div>
</div>