ng-class适用于ng-repeat之外?

时间:2015-03-16 16:59:22

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

<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上添加类而不通过过滤器2x运行数据(在ul和ng-repeat上)?

2 个答案:

答案 0 :(得分:1)

您可以在回调函数中创建过滤器并检查其长度,并根据结果更改标记。

<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;
     }
}

答案 1 :(得分:0)

您可以使用$index之类的内容和名为$scope.selected的变量并使用 ng-class = "'highlight': $index === selected"