Christian Phillips
Christian Phillips

Reputation: 18769

Angular NgClass with multiple expressions

I want to evaluate multiple expressions, but the class applied will be the same. Here's an example...

<tr ng-class="{'highlight' : Expression1 || Expression2 || Expression3}">

If any of the expressions are true, it will apply the highlight class. I have tried a few versions, but I'm not sure if this is the correct approach.

<tr ng-class="{'highlight' : Expression1, 'highlight' : Expression2 }">
<tr ng-class="[{'highlight' : Expression1, 'highlight' : Expression2 }]">

None of these seem to work, and all the example I have seen apply a different class to each evaluation.

Upvotes: 0

Views: 665

Answers (3)

Shamal Perera
Shamal Perera

Reputation: 1681

Check the version of angular library you are using.

Upvotes: 0

Mathew Berg
Mathew Berg

Reputation: 28750

This is just fine and I believe the correct way to do it.

<tr ng-class="{'highlight' : Expression1 || Expression2 || Expression3}">

Upvotes: 2

Jelmer
Jelmer

Reputation: 1009

Would this be of use?

<!-- example with string syntax -->
<!-- use the type variable as a class -->
<div class="item ng-class:type;">Stuff Goes Here</div>

<!-- example with string syntax -->
<!-- use the styleOne and styleTwo variables as classes -->
<div class="item ng-class:[styleOne, styleTwo];">Stuff Goes Here</div>

<!-- example with evaluated data -->
<!-- add the text-error class if wrong is true -->
<div class="item ng-class:{ 'text-error': wrong };">Stuff Goes Here</div>

Taken from: The many ways to use ng-class

Upvotes: 0

Related Questions