如何打破ng-repeat循环?

时间:2017-05-06 10:45:07

标签: angularjs angularjs-ng-repeat angular-loopback

以下案例需要从ng-repeat循环中突破

<div ng-init="myarr=['abc','xyz','pqr','mno','rst']">
        <div ng-repeat="item in myarr">
             {{item}}
             <ng-container ng-if="item=='pqr'">
                  <!--need to break this loop-->
             </ng-container>
        </div>
    </div>

1 个答案:

答案 0 :(得分:1)

您可以使用itemmyarr的索引来解决此问题

<ng-container ng-if="(myarr.indexOf(item) < myarr.indexOf('pqr'))">
     {{item}}
</ng-container>

here is a fiddle

如果案例是一个对象数组

例如,如果array是

`$scope.myarr = [{t:'abc',i:1},
{t:'ghj',i:2},
{t:'rty',i:3},
{t:'qwe',i:4}]`
HTML中的

<ng-container ng-if="(myarr.indexOf(item) < breakVar)">
      {{item.t + ' ' + item.i}} 
</ng-container>

并在控制器中:

 $scope.breakVar = $scope.myarr.map(function(x) {return x.i;}).indexOf(3);

对象数组fiddle