Scipion Scipion - 5 months ago 21
HTML Question

Angular2 Doing an else with ngClass

I have the following template :

<p [ngClass]="{checked: condition, unchecked: !condition}">


While this is working, I find it a bit ugly as I have to repeat twice the condition. Is there a way to something like :
[ngClass]={condition ? checked : unchecked}
(which is not working)

Thanks

Answer

Indeed

<p class="{{condition ? 'checked' : 'unchecked'}}">

or

<p [ngClass]="condition ? 'checked' : 'unchecked'">

or

<p [ngClass]="[condition ? 'checked' : 'unchecked']">
Comments