Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle Class to only one button in angular 4

I have two buttons I want to toggle class to the currently clicked button. The condition is user only select one button at a time and the user can also deselect both buttons. Here is my code stackblitz example. I have toggle class but now I am facing problem to deselect selected button. Please help.

like image 210
vedankita kumbhar Avatar asked Jan 03 '23 01:01

vedankita kumbhar


1 Answers

HTML

<button *ngFor="let button of buttons" class="btn rounded m-4" [ngClass]="(selectedButton == button) ? 'btn-primary' : 'btn-default'" (click)="onClickButton(button)">
 <i [class]="button.class"></i>
</button>

TS

onClickButton(button): void {
 if (this.selectedButton === button) {
  this.selectedButton = null;
 } else {
 this.selectedButton = button;
 }
}
like image 152
Malindu Sandaruwan Avatar answered Jan 05 '23 04:01

Malindu Sandaruwan