在javascript中获取ng-reapeat的$ index(代码隐藏)

时间:2014-04-29 11:59:33

标签: javascript angularjs angularjs-ng-repeat

如何从代码中获取$index的实际值?

我的HTML是:

<div class="list-group nav nav-stacked">
    <a 
        ng-repeat="data in dbdata | orderBy:'heading' | filter:tocsearch"
        ng-click="jumpToManual(data.user_manual_id)"
        class="list-group-item"
        ng-class="{'active':data.user_manual_id===manualdata.page_id, 'selected': selectedIndex}">
        {{data.heading}}
    </a>
</div>
<button class="btn btn-default pull-right" ng-click="nextManual(data.$index)" type="button">Next &rarr;</button>

目标是使用nextManual函数在ng-repeat之外实现一个按钮,其中ng-repeat的$ index被传递。

1 个答案:

答案 0 :(得分:1)

您可以将$parent.$index作为参数传递给ngClick函数:

<div class="list-group nav nav-stacked">
    <a ng-repeat="data in dbdata | orderBy:'heading' | filter:tocsearch" 
       ng-click="jumpToManual(data.user_manual_id, $parent.$index)" 
       class="list-group-item" 
       ng-class="{'active':data.user_manual_id===manualdata.page_id, 'selected': selectedIndex}">
       {{data.heading}}
    </a>
</div>