Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Change Font Awesome icon color on click

I created a component named "like" with the following HTML:

<div (click)="onClick()">
   <i class="fas fa-heart" [class.active]="isActive"></i>
</div>

When I click on the icon, it should change the variable "isActive" and consequently the color of the icon should also change. Here is the .ts:

onClick() {
    this.isActive = !this.isActive;
  }

CSS:

.fa-heart {
    color: #ccc;
}

.fa-heart.active {
    color: deeppink;
}

However, the "active" class is not being added or removed when I click. Why?

like image 710
mgiurni Avatar asked Oct 16 '22 21:10

mgiurni


1 Answers

You should use the following syntax:

<div (click)="onClick()">
   <i class="fas fa-heart" [ngClass]="{'active': isActive}"></i>
</div>
like image 169
Vitalii Chmovzh Avatar answered Oct 21 '22 06:10

Vitalii Chmovzh