AngularJS - 如何 highlight/add class 达到最大值

AngularJS - how to highlight/add class to a max value

假设我有一个非常简单的数据,我用 ng-repeat 循环遍历。

现在,我如何突出显示(添加 css class)到数据中的最高值。

数据:

$scope.marks = [
        {point:'11'},
        {point:'2'},
        {point:'23'}
];

html:

<ul>
  <li ng-repeat="value in marks" class={{here add class for value 23}}> //how to add class to li with max value, in this case it is 23
    {{ value.point}}
  </li>
</ul>

非常感谢您的帮助

PLUNKR

您可以使用手表计算最大值:

$scope.$watchCollection('marks', function(items) {
  $scope.maxPoint = -1; // assuming we won't have negative values
  angular.forEach(items, function(item) {
    if (parseInt(item.point) > $scope.maxPoint) {
      $scope.maxPoint = item.point;
    }
  });
});

然后在标记中使用 ngClass 指令:

<li ng-repeat="value in marks" ng-class="{max : value.point == maxPoint}">
    {{ value.point}}
</li>

就像 Petr 指出的那样,这不会很好地执行。计算变化的最大值是更好的方法。这是一个有效的插件:http://plnkr.co/edit/F8yjoWR0ZWVF4tcck1rH?p=preview