10

I got this code:

ng-class="{selectedHeader: key == selectedCol}"

It works but I would like too add the 'key' value as a class too, Ive tried:

ng-class="[{selectedHeader: key == selectedCol}, key]"

But that wont work, does anyone know how to solve this?

8 Answers 8

5

If you always want key to be added as a class:

ng-class="{selectedHeader: key == selectedCol, key: true]"

or you can just put it into a class attribute with {{}} interpolation.

ng-class="{selectedHeader: key == selectedCol}" class="{{key}}"

Just for completeness, if you only want to include it if it's equal to selectedCol:

ng-class="{selectedHeader: key == selectedCol, key: key == selectedCol}"
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but the problem is, key: true and key: key == selectedCol will just add the class 'key' and not the value. You woud have thought class="{{key}}" would work and it almost works. But for some reason, other classes like ng-scope ng-binding ui-resizable, wont be added if I go with the class="{{key}}"....No idea why
Ah... you're right about the key:true: it will add the class key. Not sure about the other missing classes though.
Can you post the details about why you need the other classes on this element, and what problems are causes by them not being on the element?
Why would be quite a long post, and some of them seems to be added by default, so not really sure. Anyway, Ive done some looking around and found this: github.com/angular/angular.js/pull/4807, so I guess you cant really do this yet...I did however found another solution, its not as nice, but it works: jsfiddle.net/pkozlowski_opensource/XVAcd/1
3

You could handle this using $scope methods to define those dynamic class names. This would look something like this:

Controller:

$scope.selectedCol = 3;
$scope.key = 3;

// dynamic ng-class values
$scope.selectedHeader = function () {
    if ($scope.selectedCol === $scope.key)
        return 'header-selected';
    else
        return false;
};

// selected header definition for ng-class
$scope.headerKey = function () {
    return 'header-' + $scope.key;
};

View:

<header ng-class="[ selectedHeader(), headerKey() ]">
    <h1>Header element</h1>
</header>

Result:

<header ng-class="[ selectedHeader(), headerKey() ]" class="header-selected header-3">
        <h1>Header element</h1>
</header>

Comments

2

// single class  
<div ng-class="{'myclass' : condition}">  
</div>  
  
// multiple class  
<div ng-class="{'myclass1': condition1, 'myclass2': condition2}">  
 some content  
</div>  

Comments

1

You can do it by

  ng-class="{selectedHeader: key == selectedCol}" class="{{key}}"

2 Comments

Thanks for taking your time, and as I commented on the other answer:You woud have thought class="{{key}}" would work and it almost works. But for some reason, other classes like ng-scope ng-binding ui-resizable, wont be added if I go with the class="{{key}}"....No idea why
You mean to add directive as class
1

I think what you are looking for is: ng-class="{ {{key}}:{{key}} }"

Also keep in mind that css classes should not begin with a number, so you may need to prefix your key value.

Comments

1

This will not work because { selectedHeader : key == selectedCol } will be evaluated as an object and not as a string:

ng-class="[{selectedHeader: key == selectedCol}, key]"

The following code work, it will add the classes 'selectedHeader' and value of key:

ng-class="[key == selectedCol ? 'selectedHeader' : '', key]"

Comments

0

Try this:

class="{{key}} ng-class:{'selectedHeader': key == selectedCol};

Comments

0

I have a array of classes (vm.classList), and needed to dynamically add a conditional class ('required-border') to a table. This is what worked for me.

In the controller:

vm.classList = [ 'base-table', 'medium-height' ];

In the template:

ng-class="[{ 'required-border': vm.isRequired && vm.rowCount == 0 }, vm.classList ]"

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.