我的 Controller 的 $scope
上有一个数组 ...
$scope.myElements = [false, false, true, false, true, false];
... 如果所述数组中的至少一个元素为真,则 div 标签上的类应为 firstClass
,否则为 secondClass
。使用 ng-class
很容易实现 ...
<div ng-class="anyElementTrue() ? 'firstClass' : 'secondClass'">
...和辅助函数...
$scope.anyElementTrue = function () {
for (var i = 0; i < $scope.myElements.length; i++) {
if ($scope.myElements[i]) return true;
}
return false;
}
是否有更简单的方法可以仅使用表达式来实现相同的目的?像这样:
<div ng-class="angular.any(myElements) ? 'firstClass' : 'secondClass'">
除了there is no angular.any
.想法?
最佳答案
你可以这样做:
<div ng-class="myElements.indexOf(true) > -1 ? 'firstClass' : 'secondClass'">
关于javascript - 在 ng 类中的数组上使用的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28373430/