20

Here are 2 different ways to use the ng-class directive. I need them both, on the same element, but that doesn't work.

Using an object in ng-class

http://plnkr.co/edit/uZNK7I?p=preview

<div ng-repeat="item in [1, 2, 3, 4, 5]" 
     ng-class="{ first: $first, last: $last }">{{item}}</div>

correctly results in

<div class="first">1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div class="last">5</div>

Using an expression in ng-class

http://plnkr.co/edit/tp6lgR?p=preview

<div ng-repeat="item in [1, 2, 3, 4, 5]" 
     ng-class=" 'count-' + ($index + 1) ">{{item}}</div>

correctly results in

<div class="count-1">1</div>
<div class="count-2">2</div>
<div class="count-3">3</div>
<div class="count-4">4</div>
<div class="count-5">5</div>

Now, how about use them together?

I need dynamic class names (like 'count-' + n), but also need the object syntax for multiple classes.

I can't just use 2 ng-class attributes (http://plnkr.co/edit/OLaYke?p=preview), only the first one works.

Any suggestions?

4 Answers 4

20

You shouldn't be using ng-class for the second one, use

<div class="count-{{$index + 1}}" ng-class="{ first: $first, last: $last }">
6
  • This seems to work (plnkr.co/edit/OLaYke?p=preview), although I'm not sure about ie8 (and unfortunately plnkr doesn't work in ie8, and I don't have time to set this up and test it) Commented Mar 31, 2014 at 15:41
  • According to angular docs suggested solution is a bad practice: docs.angularjs.org/api/ng/directive/ngClass#known-issues See my answer below for recommended way to do it Commented Nov 13, 2016 at 13:27
  • @MaximKulikov - It doesn't say that anywhere, it says not to use interpolation. {{}} is different than the syntax displayed above.
    – tymeJV
    Commented Nov 13, 2016 at 13:40
  • 2
    @tymeJV {{$index + 1}} is an interpolation, and you suggest to use it in class attribute simultaneously with usage of ngClass directive Commented Nov 13, 2016 at 13:48
  • 2
    @tymeJV quote from documentation: > You should not use interpolation in the value of the class attribute, when using the ngClass directive on the same element. Commented Nov 13, 2016 at 13:57
8

You should provide ngClass with an array that contains both expression and object:

<div ng-class="['count-' + ($index + 1), { first: $first, last: $last }]">

Note: The solution suggested in accepted answer (simultaneous usage of ngClass directive and interpolation in class attribute) is a bad practice. See more details here and here.

2

If you need to mix them together, just go for strings concat:

ng-class="'count-' + ($index+1) + ($first?' first':($last?' last':''))"

Or for a clearer view:

ng-class="'count-' + ($index+1) + ($first?' first':'') + ($last?' last':'')"

There aren't known pros for using class="{{exp}}" instead of ng-class="exp", since both get updated when the value of exp changes. On the contrary, going with the first one might cause page froze in some cases.

1
  • Thanks, I didn't know we could use ternaries in the ng-class expression.
    – BrDaHa
    Commented Sep 18, 2014 at 17:25
0

I thing you can separate atributes ng-class with ;

try:

ng-class=" 'count-' + ($index + 1) ; { first: $first, last: $last }"
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.