lakshay lakshay - 1 month ago 22
AngularJS Question

how to write conditional statements in ng-class?

I have written the condition as

ng-class="{true: 'ion-ios-heart', false: 'ion-ios-heart-outline'}[favOffers[offer.id]]"


i.e if favOffers[offer.id] has a value then apply the class in true statement else apply the class in false statement.

But it is not working. favOffers is defined in my controller.

Answer

You can simply use a ternary condition in your case

<div ng-class="favOffers[offer.id] ? 'ion-ios-heart': 'ion-ios-heart-outline'"></div>