有办法停止执行ng-repeat

时间:2015-10-30 18:05:02

标签: angularjs

当条件满足时,我需要停止执行阻止ng-repeat,还有一些方法可以去。

类似的东西:

<div ng-reapeat="item in items">
   <div ng-if="item.age === 0">
      here I need to break the ng-repeat
   </div>
</div>

1 个答案:

答案 0 :(得分:5)

您可以创建一个在满足条件后立即中断的自定义过滤器:

var app = angular.module("App", []);

app.filter('myFilter', function() {
  return function(input) {
    var result = [];

    for (var i = 0; i < input.length; i++) {
      if (input[i] === 4) break;
      result.push(input[i]);
    }

    return result;
  };
});

app.controller("Ctrl", function($scope) {
  $scope.data =   [1, 2, 3, 4, 5, 6];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="App" ng-controller="Ctrl">
  <div ng-repeat="d in data | myFilter">
    {{d}}
  </div>
</body>