3

I am trying to display dynamic values within divs in tile format and adding particular classes to these divisions according to the statusIds [which I get in the response object].

I can achieve this with following code,

<div class="tile-card p-2" style="cursor: pointer;" [ngClass]="{'card-open': SRcounts.statusId == 1,
        'card-wait': SRcounts.statusId == 2,
        'card-assign': SRcounts.statusId == 3,
        'card-vip': SRcounts.statusId == 4,
        'card-email': SRcounts.statusId == 14,
        'card-voice': SRcounts.statusId == 7,
        'card-nms': SRcounts.statusId == 8,
        'card-requested': SRcounts.statusId == 9,
        'card-approval': SRcounts.statusId == 10,
        'card-subassign': SRcounts.statusId == 11,
        'card-change-1': SRcounts.statusId == 12,
        'card-change-2': SRcounts.statusId ==13,
        'card-change-3': SRcounts.statusId ==16,
        'card-change-4': SRcounts.statusId ==15,'card-change-5': SRcounts.statusId ==25}" data-toggle="tooltip" title="{{SRcounts.statusLable}}">
        <p class="status">{{SRcounts.statusLable | slice:0:11}}..</p>
        <p class="number">{{SRcounts.statusCount | number}}</p>
      </div>

but I want to reduce these multiple checks, Is there a way where I can take statusId in my classname?

how can I achieve [ngClass]="{'card-status-'+SRcounts.statusId}"

1 Answers1

0

Maybe this works:

//define an array in the component with the css class names
SRcountsStatuses = ['card-open', 'card-wait', 'card-assign', /* etc...*/];

//in the template, access the class name you need by its index
[ngClass]="SRcountsStatuses[SRcounts.statusId]"
Christian Benseler
  • 7,907
  • 8
  • 40
  • 71