I'm very new to Angular.
I want to apply CSS (active class) to a button dynamically when it is clicked and the button is not active and need to remove the active class to the button.
HTML code
<div class="tab">
<button (click)="onTabClick('0')">Sports</button>
<button (click)="onTabClick('1')">News</button>
<button (click)="onTabClick('2')">Movies</button>
</div>
<div>
<app-sports *ngIf="tabIndex == 0"></app-sports>
<app-movies *ngIf="tabIndex == 2"></app-movies>
</div>
TS file
tabIndex = 2 ;
onTabClick(index){
this.tabIndex = index;
}
CSS
/* Style the buttons that are used to open the tab content */
.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
}
/* Change background color of buttons on hover */
.tab button:hover {
background-color: #ddd;
}
/* Create an active/current tablink class */
.tab button.active {
background-color: #ccc;
}