1

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

4
  • isn't this quite similar to the example in the documentation? docs.angularjs.org/api/ng/directive/ngClass Commented Jul 4, 2014 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. Commented Jul 4, 2014 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 }" Commented Jul 4, 2014 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. Commented Jul 4, 2014 at 10:42

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.