Search code examples
angularangular-ng-class

Angular2 [ngClass] Conditional fails with Spaces


I have an ngClass conditional that functions fine on class names without spaces, but when i try to do something like below and i toggle isTrue on button click, the class changes from "btn btn-primary" to "btn-default". The space in there seems to be throwing it off. Am i missing something

[ngClass]="{ 'btn btn-default': isTrue, 'btn btn-primary': !isTrue}"

Solution

  • Take the common one out, and have the rest of the expression in an ngClass directive

    class="btn" [ngClass]="{ 'btn-default': isTrue, 'btn-primary': !isTrue}"