In0cenT
In0cenT

Reputation: 481

Material icon set color red if boolean is false

For my todo app I would like to have a thumps up/down in green/red color depending if the todo is completed or not. In the old version of the app I was able to use ngClass and set different css class to the icon.

HTML:

<mat-card class="example-card" *ngFor="let todo of todoList">
    <mat-card-header>
        <div mat-card-avatar>
            <mat-icon [ngClass]="todo.completed ? '.green' : '.red' ">thumb_up</mat-icon>
        </div>
        <mat-card-title>{{todo.taskName}}</mat-card-title>
        <mat-card-subtitle>
            <mat-icon style="font-size: 15px">calendar_today</mat-icon>
            {{todo.dueDate | date: 'dd.MM.yyyy'}}</mat-card-subtitle>
    </mat-card-header>
    <mat-card-content>
        <p>
            {{todo.extraNote}}
        </p>
    </mat-card-content>
    <mat-card-actions align="center">
        <button mat-button>
            <mat-icon>done</mat-icon>
        </button>
        <button mat-button>
            <mat-icon>edit</mat-icon>
        </button>
        <button mat-button (click)="delete(todo.id)">
            <mat-icon>delete</mat-icon>
        </button>
    </mat-card-actions>
</mat-card>

CSS:

.example-card {
    max-width: 90%;
    margin-top: 10px;
    margin-left: auto;
    margin-right: auto;
}


.red {
    font-size: 30px;
    color: red
}

.green {
    font-size: 30px;
    color: green;
}

Why doesn't it work in this scenario?

Thanks for any help!

Upvotes: 0

Views: 1334

Answers (1)

SiddAjmera
SiddAjmera

Reputation: 39482

Just get rid of the . in the class names in your [ngClass] syntax:

Change

<mat-icon [ngClass]="todo.completed ? '.green' : '.red' ">thumb_up</mat-icon>

to

<mat-icon [ngClass]="todo.completed ? 'green' : 'red' ">thumb_up</mat-icon>

Upvotes: 4

Related Questions