角度,限制子重复

时间:2015-01-22 18:16:30

标签: javascript angularjs angularjs-ng-repeat ng-repeat

我试图通过其父重复的索引来限制子重复。因此,它的水平指数将限制在那个(+ 1所以我们不从0开始)。这是我的想法 -

        <div class="inputRepeater" ng-repeat="face in inputFaces" ng-show="face.isCurrent" >
            <div class="trialGrid">
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th ng-repeat="(key, val) in rowCollection[0]">{{key}}</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr ng-repeat="row in rowCollection | limitTo: face.$index + 1">
                        <td ng-repeat="item in row">{{item}}</td>
                    </tr>
                    </tbody>
                </table>
            </div>
</div>

所以表中的tr将限制为face的初始重复,buy faces $ index + 1.这不起作用。任何见解都会非常感激。谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用ng-init保存对上限$ index的引用

    <div class="inputRepeater" ng-repeat="face in inputFaces" ng-show="face.isCurrent" >
        <div class="trialGrid" ng-init="$faceIndex = $index">
            <table class="table table-striped">
                <thead>
                <tr>
                    <th ng-repeat="(key, val) in rowCollection[0]">{{key}}</th>
                </tr>
                </thead>
                <tbody>
                <tr ng-repeat="row in rowCollection | limitTo: $faceIndex + 1">
                    <td ng-repeat="item in row">{{item}}</td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>

答案 1 :(得分:1)

而不是face.$index使用$parent.$index,因为您想引用父ng-repeat $ index。 因为ng-repeat从其当前运行范围创建了一个独立的范围。

<强> CODE

<div class="inputRepeater" ng-repeat="face in inputFaces" ng-show="face.isCurrent">
    <div class="trialGrid">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th ng-repeat="(key, val) in rowCollection[0]">{{key}}</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="row in rowCollection | limitTo: $parent.$index + 1">
                    <td ng-repeat="item in row">{{item}}</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

希望这会对你有所帮助。