0

How to change angular button color based on condition ? , this is a mat icon button

#code

<div class="flex">
  <button mat-icon-button *ngFor="let action of actions" 
    [disabled]="isDisabled && action.name === 'For Approval'" matTooltip="{{action.name}}"
    (click)="clickActionBtn(action.name)">
    <mat-icon style="font-size: 20px;" class="{{action.class}}">
      {{action.icon}}
    </mat-icon>
  </button>

I tried , but it does not work

[class.gray]="isDisabled

1 Answer 1

3

Angular Material mat-icon takes color as an input (with primary, accent, and warn options based on your material theme colors) to define the color of the icon, so you need to use this input to change it, like the following:

<!-- `color` input allowed options are: `primary`, `accent`, and `warn` only. -->
<mat-icon [color]="isDisabled && action.name === 'For Approval' ? 'primary' : ''"></mat-icon>

So you can update your button HTML to be like the following:

<button
  mat-icon-button
  *ngFor="let action of actions"
  [disabled]="isDisabled && action.name === 'For Approval'"
  matTooltip="{{action.name}}"
  (click)="clickActionBtn(action.name)"
>
  <mat-icon
    style="font-size: 20px"
    [color]="isDisabled && action.name === 'For Approval' ? 'primary' : ''"
  >
    {{action.icon}}
  </mat-icon>
</button>

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

9 Comments

and how would that be based on condition ? I only want to change the color icon of the button if condition is true
I updated my answer to include a condition.
how do I insert this condition isDisabled && action.name === 'For Approval'
Check it now, please.
I am using the class with the color , imgur.com/a/t2iic01 , it is possible that we add the color on the json config ?
|

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.