Search code examples
material-designangular6angular-ng-class

Button color change based on condition in ngclass in Angular6


If i click the button it will change the color in to orange but by requirement is after click the same button it need to change into default color. so please guid me to do this.

  <button #val1 class="mat-raised-button" [ngClass]="{'orange': val1.value==current_product_size}" (click)="sizeFilter(val1.value)" value="375ML">375ML</button>

Component.ts

sizeFilter(size_clicked) {
    this.current_product_size = size_clicked
}

Solution

  • **You can give more than one ngClasses if you want like this** 
    
    
    <button #val1 class="mat-raised-button" 
    [ngClass]="{'toggleColor': toggleColor,'orange': val1.value==current_product_size}" 
    (click)="sizeFilter(val1.value)" 
    value="375ML">375ML</button>
    

    Component

    toggleColor = false;
    sizeFilter(size_clicked) {
    this.toggleColor = !this.toggleColor;
    this.current_product_size = size_clicked
    }  
    

    CSS

    .toggleColor{
      background: yourDefaultColor !important;
    }