Search code examples
javascriptcssangularjsng-class

Change part of the CSS class using ngClass


I have multiple classes apply to an element. Is it possible to change only 1 class, not the all of the classes?

For example,

<span ng-class="{'result warning' : error, 'result passing' : !error}"></span>

As you can see, I have to duplicate result class in both of the conditions. Is there a way to not have to repeat it?

Thanks!


Solution

  • <span class="result" ng-class="{'warning': error, 'passing': !error}"></span>
    

    ng-class can add/remove classes - it doesn't over-write existing classes unless they are specified in ng-class.