Search code examples
angularangular-directive

Variable value in ngClass directive - Angular 5


I have this ngClass directive to give a certain colour to a certain field of a table:

[ngClass]="{'sem_exam_ini' : row.dias_expir >= 9999, 'exam_prox_expir' : row.dias_expir > -90 && row.dias_expir < 0, 'exam_expir' : row.dias_expir > 0 }" 

That row.dias_expir > -90 it's not a fixed value, rather a variable number.

I tried to concatenate strings and binding values but throws errors. Every answer on the Internet didn't solve anything...

I wonder if I can add a kind of binding or string concatenation in order to add a variable there. It's possible?

Many thanks


Solution

  • You can change that -90 for a variable value inside your class without problem, for example calling it 'offset'

    [ngClass]="{'sem_exam_ini' : row.dias_expir >= 9999, 'exam_prox_expir' : row.dias_expir > offset && row.dias_expir < 0, 'exam_expir' : row.dias_expir > 0 }"
    

    Check this fiddle with variable values for both https://jsfiddle.net/1hk7knwq/10887/