Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add class to existing classes using conditional [class]

I'm using an ionic ion-icon

<ion-icon name="checkmark" [class]="condition ? 'class1' : ''"></ion-icon>

I'd like to add an extra class if the condition is true, but dont add the extra class if the condition is false.

Problem is, if the condition is false it removes all the pre-defined classes on the icon from Ionic Framework.

like image 867
Kim Avatar asked Feb 01 '26 12:02

Kim


1 Answers

Try to avoid binding to class directly.

Use either

[class.class1]="condition"

or

[ngClass]="condition ? 'class1' : ''"

With binding to class you explicitly bind the whole property value, instead of a single class.

like image 65
Günter Zöchbauer Avatar answered Feb 04 '26 02:02

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!