【问题标题】:Nested ng-repeat $index is not working right嵌套的 ng-repeat $index 无法正常工作
【发布时间】:2016-04-30 23:11:12
【问题描述】:

在这里,我想创建一个每次 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>

【问题讨论】:

  • 大块代码与问题有何关系?它没有 ng-repeat,也没有 $index。澄清您的问题,发布相关代码,准确解释它产生的输出以及您想要的输出。
  • 很抱歉出现复制粘贴问题

标签: angularjs angularjs-ng-repeat


【解决方案1】:
<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>

【讨论】:

    猜你喜欢
    • 2019-01-07
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多