I am currently working on implementing dynamic background coloring for a span tag in my Angular view that displays document types. The code snippet provided is as follows:
<mat-card *ngFor="let record of records">
<span class="doc-id">
Document ID: {{ record.id }}
</span>
<span>
{{ record.documentType }}
</span>
</mat-card>
The record.documentType
contains string values retrieved from the backend. The objective is to assign different random background colors based on the document type.
For instance:
If the documentType is 'employee', all employee documents should have a specific color (e.g., green). Similarly, if it is 'salary', salary documents should have another random color (e.g., yellow).
I have experimented with various solutions found online, but none have met my requirements adequately.
My current approach involves passing the documentType value to a TypeScript function, which will generate a new random color if one has not already been generated for that type.
[ngStyle]="generateRandomBackgroundColor(record.documentType)
Would you happen to have any suggestions or best practices for addressing this particular issue? I am utilizing Sass within the project, so any related suggestions are greatly appreciated.
Thank you in advance!