Changing background color using ngClass and boolean is not working correctly?

Question

I am trying to check an object coming in a back-end response to see if error.isCritical is true or false and setting the ngClass accordingly, i've tried many different approaches, but this is the one i am using right now:

Angular/HTML:

`<p-table>
    <ng-template pTemplate="header">
        <tr *ngFor="let error of errors"
            [ngClass]="{critical: error.isCritical, notCritical:
            !error.isCritical}">
             <td width="25%">
                {{ error.dateRange['start'] }} to
                {{ error.dateRange['end'] }}
             </td>
             <td width="25%">{{ error.processId }}</td>
             <td width="50%">{{ error.message }}</td>
        </tr>
     </ng-template>
</p-table>`

CSS:

`.critical{
  background-color: red;
}
.notCritical{
  overflow: hidden;
  background-color: white;
  padding: 5px;
  color: black;
}`

It seems to either make them all red background, or all white background no matter what i try, and i have checked the backend call and the isCritical flag is false for some and true for others, so i can't figure out why they are all red when they are red?

Any Help would be greatly appreciated.

Answer

You can dynamically add classes using the code below:

`[ngClass]="{'critical': error.isCritical, 'notCritical' : !error.isCritical }"`

This answer was originally posted on Stack Overflow. You can find the full discussion here

Related Posts

Transfer git repositories from GitLab to GitHub - can we, how to and pitfalls (if any)?

## Question Can one transfer repositories from GitLab to GitHub if the need be. If so, how exactly can I go about doing the same? Also, are there any pitfalls in doing so or precautionary measures

Read More

Cannot set headers after they are sent to the client - error

## Question Error `[ERR_HTTP_HEADERS_SENT]`: Cannot set headers after they are sent to the client ```text `at ServerResponse.setHeader (_http_outgoing.js:558:11) at ServerResponse.header (D:\D

Read More

Pulling data with 'Where' and 'Include' statements at the same time

## Question I have managed to get my include statements working with my foreign keys however when I try to add a 'where' statement to the findAll statement I am getting the below error. I have check

Read More