I have a list group and I want to add a css class when one item is selected using ngClass directive
how to verify is the item is selected?
I only have one option.. this is a sidebar component, and when I pick an option, it has to send it to the parent (I did that with @Output) and also have a css class on selected option...
this is my component.html
<ul class="list-group">
<li *ngFor="let option of options" (click)="selectOption(option)" class="list-group-item" [ngClass]="option ? 'activate-class' : 'deactivate-class'">
{{option.title}}
</li>
</ul>
component.ts
@Output() optionSelected: EventEmitter<any> = new EventEmitter();
options = [
{
title: "Activate account",
value: NavigationOptions.ACTIVATE_VENDOR
},
{
title: "Deactivate account",
value: NavigationOptions.DEACTIVATE_VENDOR
}
];
constructor() { }
ngOnInit() {
}
selectOption(option) {
this.optionSelected.emit(option);
}
isOptionSelected(option)in your component, and use it in your template.selectOption(option)