0

I'm trying to construct a web page using angular material where I have a drop-down select element and a bunch of FAB buttons to show status. What I would like to do is that if a click on the dropdown list and select an item. The background color of the button is changed based on the item I select.

I've found a few solutions to change color(scheme) based on button click event. Is there a way to trigger the background color change based on the drop-down list selection?

Thanks

JustAG33K
  • 1,403
  • 3
  • 13
  • 28
Jin Ma
  • 169
  • 2
  • 12

2 Answers2

0

You can trigger the function that changes the button color each time the item selected has been changed using this code :

  <mat-select (selectionChange)="setButtonColor($event.value)"
              name="color" formControlName="type" >
       <mat-option *ngFor="let color of colors" 
                   [value]="color">{{type}}
        </mat-option>
   </mat-select>

   <button mat-button [ngStyle]="{'background-color':bgColor}">
      Button
   </button>

your Typescript file should contain a variable bgColor to hold the selected value string and the setButtonColor function that change the value of this variable

bgColor = 'blue'; // blue is the default color in this case

setButtonColor(color){
    bgColor = color;
}

0

Angular Mat Select Multiple selectionchange finding which option was changed

The answer to the above question helped me. https://stackblitz.com/edit/angular-1e9gsd-34hrwg?file=app/select-overview-example.html I tweaked my code based on the code shown above and it all worked.

Jin Ma
  • 169
  • 2
  • 12