Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a table where I need to apply two different classes, using expressions. 1st class is applied based on following expression. {'Up':'class-up', 'Down':'class-down'}[a.status] and 2nd class is applied based on bold: !a.read

The classes here are class-up, class-down, bold. So how should be the expression framed? I tried:

<tr ng-repeat="a in all" ng-class="{{'Up':'class-up', 'Down':'class-down'}[a.status],bold: !a.read}">

<tr ng-repeat="a in all" ng-class="{'Up':'class-up', 'Down':'class-down'}[a.status],bold: !a.read">

But I keep getting errors in console. What is the correct format to apply these classes based on the given expressions

share|improve this question
    
isn't this quite similar to the example in the documentation? docs.angularjs.org/api/ng/directive/ngClass –  Eduard Gamonal Jul 4 at 10:22
    
Yes, I tried those. But I'm unable to get it to work. The problem comes when 1st expression isn't directly dependent on a variable. {'Up':'class-up', 'Down':'class-down'}[a.status] . Here a.status returns Up or Down as response which in turns applies class-up and class-down classes respectively. –  Aniket Sinha Jul 4 at 10:30
1  
Do you even need such a complex expression? Can you just use something like: ng-class="{ 'class-up': a.status === 'Up', 'class-down': a.status === 'Down', 'bold': !item.read }" –  Paolo Moretti Jul 4 at 10:34
    
This is the final approach which I tried(works) i.e splitting into status as Up and Down in 2 components. I thought maybe status can be evaluated in a single expression instead of two. –  Aniket Sinha Jul 4 at 10:42

1 Answer 1

up vote 1 down vote accepted

With the clarification from your comment:

<tr ng-repeat="a in all" ng-class="{'class-up': a.status=='up', 'class-down': a.status=='down', 'bold': !a.read}">hello world</tr>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.