I am facing an issue where I need to dynamically assign a class to a div element within a *ngFor loop based on a method. Below is the code snippet from my HTML file:
<ng-container *ngFor="let data of totalData let j = index">
<div>
<a (click)="selectData(data)">
<div [ngClass]="{selected : selectedCodeMethod(data)}">
{{data.code}}
</div>
</a>
</div>
</ng-container>
Furthermore, here is the TypeScript code I am using:
selectedCodeMethod(data){
if(this.selectedCode.includes(data.code)){
return true;
}
return false;
}
selectData(data){
this.selectedCode.push(data.code);
}
The problem I am facing is that even when the selectedCode array is updated by the selectedCodeMethod() function, the changes are not reflected in the ngClass directive. Can someone please suggest what changes I need to make to fix this issue?