1

I would like to create class names based on an the evaluation of an angular expression, but it seems that angular is removing these expressions from the eventually compiled classes. When iterating over cells, assume the following code:

<td ng-class="{'label{{cell.count}}': !!cell.count}"></td>

My goal is that when cell.count = 1 the resulting class will be "label1", when cell.count = 2, "label2", etc. In practice, what happens is that angular discards the expression in the eventual class - As long as !!cell.count, I get the class "label" regardless of the count. How can I achieve this?

1

3 Answers 3

2

Try this:

<td ng-class="{'label'+cell.count: !!cell.count}"></td>

If it gets too complicated you can also move the entire object out into a function in the scope.

<td ng-class="getCellClass()"></td>

and then:

$scope.getCellClass = function() { ... }

Your problem is that the {{...}} substitution isn't appropriate here. You have to use an angular expression to create the object, or if you call a method on the scope you can do any javascript you need to create it.

Sign up to request clarification or add additional context in comments.

1 Comment

'label'+cell.count does not work, angular does not approve of the plus sign here, and does not concatenate strings. Regarding your second suggestion - that's what I'm trying to avoid, I don't want to place my CSS classes logic inside of the controller, and was hoping to find a way around it
0

Did you ever try: <td ng-class="'label' + cell.count"></td> - simply not using the curly braces works for me in my project (angular 1.5).

Comments

0

I had the same issue. I managed to solve it like this:

<td ng-class="{['label' + cell.count]: !!cell.count}"></td>

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.