• Skip to main content
  • Skip to primary sidebar

Web Development Archive

  • Archive
You are here: Home / Other / Add active class only to the selected card

Add active class only to the selected card

Child Component

export class ChildComponent {
@Input() public isActive: boolean = false;
@Output() public statClick = new EventEmitter<Event>();

public get classes(): string[] {
const active = this.isActive ? 'isActive' : '';
return [active];
}

public onStatClick(): void {
this.statClick.emit();
}
}
<div class="card" [ngClass]="{ active: isActive }" (click)="onStatClick()"></div>

Parent Component

export class ParentComponent  {

public activeItem: ChartStat | null = null;

@Input() public isActive: boolean = false;

public onStatClick(clickedItem: ChartStat): void {
this.activeItem = clickedItem;
}
}
<app-child
[isActive]="item === activeItem"
(statClick)="onStatClick(item)"
/>

Filed Under: Other

About Gabor Flamich

I'm a web developer and designer based in Budapest, Hungary. In recent years, I've documented hundreds of solutions I came across during development. This site is an archive for useful code snippets on WordPress, Genesis Framework and WooCommerce. If You have any questions related to WordPress development, get in touch!

Primary Sidebar

  • angular.io
© 2026 WP Flames - All Right Reserved