Search code examples
javascriptangularjsangularjs-directiveng-class

AngularJs: ngClass with multiple statements


I need to add two classes through one ng-class definition

One looks like

{true: 'bar', false: ''}[someVar === 'bar']

And the other looks like

{foo: someOtherVar === 'foo'}

If I join them

<div ng-class="{true: 'bar', false: ''}[someVar === 'bar'],{foo: someOtherVar === 'foo'}">

it doesn't work. What is the correct syntax ?


Solution

  • here you can do it like this:

    <div ng-class="{ 'bar' : someVar === 'bar', 'foo' : someOtherVar === 'bar' }"></div>
    

    Just for brevity you could also do this if you need to add two claases and are checking just one variable.

    <div ng-class="{ 'bar foo' : expression1 }"></div>