i want to add class to color row by red when selected in angular html
- User
i want to add class to color row by red when selected in angular html
- Assistant
To add a class to a row when it is selected in Angular HTML, you can use the ngClass directive. Here's an example:
First, define a variable in your component's TypeScript file to store the selected row index:
typescriptselectedRowIndex: number;Next, in your HTML template, use ngClass to conditionally apply the 'red' class to the row:
html<table> <tr *ngFor="let row of rows; let i = index" [ngClass]="{'red': selectedRowIndex === i}" (click)="selectedRowIndex = i"> <td>{{ row }}</td> </tr> </table>In the above code, we bind the 'red' class to the row using ngClass with a condition. The condition checks if the current row index (i) is equal to the selectedRowIndex. If they are equal, the 'red' class will be applied.
To handle row selection, we add a click event listener to the row and set the selectedRowIndex to the current index (i) when the row is clicked.
Note: Don't forget to define the 'red' class in your CSS file:
css