1

I'm trying to add two classes to this div using ng-class, but even though checkForActive is returning true, it's being ignored and only class_{{$index}} is getting added.

If I remove class_{{$index}} altogether, active is added correctly.

Is there an obvious mistake in my syntax here?

<div "ng-class="{active: checkForActive, disabled: checkForDisable, class_{{$index}}} "></div>

1 Answer 1

2

You could just provide true value to the key class_{{$index}} just to that property gets added as a class name to the class list of the element. It is just the way you do active: checkForActive.

i.e

{active: checkForActive, disabled: checkForDisable, class_{{$index}} :true}

But i believe there could be some undesired behavior due the usage of interpolation ({{) within the ng-class directive (Atleast used to happen with older versions). So you could as well use an array.

ng-class="[checkForActive && 'active' , checkForDisable && 'disabled', 'class_' + $index]"

The above method will add a class name false if active or disabled is false, which should be harmless.

Or pass index to a controller function say getStatus($index) and return the object from there and use it in the ng-Class directive.

 $scope.getClass = function(){
     var obj = {active: $scope.checkForActive, disabled: $scope.checkForDisable}; 
     obj['class_' + this.$index] = true;
     return obj;
 }

and

 ng-class="getClass()"

@Okazari pointed out that it indeed works by mixing class with ng-class so you could also do:

class="class_{{$index}}" ng-class="{active: checkForActive, disabled: checkForDisable}"
5
  • This is a very helpful answer, much appreciated.
    – Daft
    Commented Jun 25, 2015 at 15:30
  • @Okazari It would work atleast in the first digest cycle. But if i remember correctly, ng-class watch will be set on derived value ex: class_1 instead of class_{{$index}} (atleast with very old versions probably with 1.2). So if you remove or add an item it wont change. But not sure if this issue is no more with newer versions of angular though.
    – PSL
    Commented Jun 25, 2015 at 15:54
  • @Okazari Sorry above comment was based on ng-class=".." just saw you are asking about class=".." which would work as well but you cannot mix it with ng-class which provides a lot of flexibility.
    – PSL
    Commented Jun 25, 2015 at 15:57
  • Just made a tiny plunker about this plnkr.co/edit/Mi0EGvsqze6B8kwENL2d?p=preview looks like all is working fine. If i remember well using ng-class will just add classes into the class property of the element in addition to the already placed class.
    – Okazari
    Commented Jun 25, 2015 at 16:04
  • @Okazari Thanks for the plnkr.. Interseting. Probably angular version. Because i remember when i upgraded to 1.3.x in one of my applications i had this issue where while using interpolation inside a class ng-class was failing later while debugging i had found out that it is the ng-class that was the culprit and a similar error was logged in git as well. And that does not happen for simple scenarios, i had a bit of complex scenario in my application.
    – PSL
    Commented Jun 25, 2015 at 16:13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.