Within my controller's $scope
, there is an array called myElements
...
$scope.myElements = [false, false, true, false, true, false];
...and I want to assign the class firstClass
to a div element if any of the elements in the array are true, otherwise secondClass
. This can be easily achieved using ng-class
...
<div ng-class="anyElementTrue() ? 'firstClass' : 'secondClass'">
...along with a helper function...
$scope.anyElementTrue = function () {
for (var i = 0; i < $scope.myElements.length; i++) {
if ($scope.myElements[i]) return true;
}
return false;
}
Is there a simpler way to accomplish this only using an expression? Something like:
<div ng-class="angular.any(myElements) ? 'firstClass' : 'secondClass'">
Unfortunately, there isn't a built-in angular.any
method. Any ideas?