2

The color of my mat icon element should change dynamically.

<mat-icon class="temperature icon" svgIcon="thermometer-alert" matTooltip = {{temp_tooltip}}
          aria-label="Icon that displays a tooltip when focused or hovered over"
          [ngClass]= "{
            'safe' : 23 <= temp_tooltip <= 24.5,
            'alarm_lowside' : 21 <= temp_tooltip < 23,
            'alarm_highside' : 24.5 < temp_tooltip <= 26, 
            'critical_lowside' : temp_tooltip < 21,
            'critical_highside' : temp_tooltip > 26
           }" >
        </mat-icon>

temp_tooltip value is binded in component.ts

and css have the color styling

.safe {
 color: green
}

.alarm_lowside {
    color: yellow
}

.alarm_highside {
    color: yellow
}

.critical_lowside {
    color: red
}

.critical_highside{
    color: red
}

it works fine with critical_lowside and critical_highside classes, but not for the other classes. How to correct. Is the way ngClass is defined is it correct?

2
  • 2
    Change your condition to this: temp_tooltip >= 23 && temp_tooltip <= 24.5, ... do the same for all. Commented Apr 17, 2019 at 14:14
  • Try this -> stackoverflow.com/a/49531806/9172846 Commented Apr 26, 2024 at 9:08

2 Answers 2

2

23 <= temp_tooltip <= 24.5 is not a correct expression in javascript/typescript.

You should find another way to phrase this condition like 23 <= temp_tooltip && temp_tooltip <= 24.5

Sign up to request clarification or add additional context in comments.

Comments

2

The correct syntax would be this:

 [ngClass]= "{
        'safe' : temp_tooltip >= 23 && temp_tooltip <= 24.5,
        'alarm_lowside' : temp_tooltip >= 21 && temp_tooltip < 23,
        'alarm_highside' : temp_tooltip > 24.5 && temp_tooltip <= 26, 
        'critical_lowside' : temp_tooltip < 21,
        'critical_highside' : temp_tooltip > 26
   }"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.