ng-class 在 ng-repeat 之外应用?

ng-class apply outside of ng-repeat?

<ul class="listing listing-sm small" ng-class="{'divider': (model.selections | filter:filter).length > 0}">
  <li ng-repeat="selection in model.selections | filter:filter" ng-class="{highlight: selection.on}">

有没有更好的方法在 ul 上添加 class 而没有 运行 数据通过过滤器 2x(在 ul 和 ng-repeat 上)?

您可以使用 $index 之类的东西和一个名为 $scope.selected 的变量,然后使用 ng-class = "'highlight': $index === selected"

您可以在回调函数中创建过滤器并检查它们的长度并根据结果相应地更改标志。

<ul class="listing listing-sm small" ng-class="{'divider':hasResults}">
    <li ng-repeat="selection in manualFilter(model.selections)" ng-class="{highlight: selection.on}">

在 js 中:

$scope.manualFilter = function(selections) {
     var result = $filter('filter')(selections);
     if (result.length > 0 ) {
              $scope.hasResults = true;
     } else {
              $scope.hasResults = false;
     }
}