Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular's ng-class: combine enumerated (array) classes and condition-based ones?

I have an array with classes set in the controller (has to be this way, it's read from page's meta-template). I'd also like to have a conditional classes applied to the same element. Is it possible?

Edit, more info: my html element has classes that originate from two sources - one is a set of classes created by the controller (they are provided by a service outside of my code, I can't have them hardcoded in the view). The other source is an output of a condition (for example: if "$scope.activeElement === name_of_this_element, add 'active' class).
In ng-class directive, I can't use an object notation for unspecified list of classes (the first source) and I have to use an object notation for conditional classes.

Edit: found a solution (see below).

like image 230
muszek Avatar asked Dec 16 '22 03:12

muszek


1 Answers

Another option is to write an ng-class in this way

<div ng-class="[item.class, item.errorClass, item.locked ? 'locked-item' : '']"></div>
like image 168
winston.wolfe Avatar answered May 16 '23 09:05

winston.wolfe