首页 > 解决方案 > Angular Material - 如何在运行时更改按钮颜色

问题描述

我正在使用有角材料制作侧边栏菜单。我没有找到根据某些组件属性更改按钮颜色的方法。

我已经阅读了文档:https ://material.angular.io/components/button/overview

关于主题,它只说:

Buttons can be colored in terms of the current theme using the color property to set the background color to primary, accent, or warn.

这是我的代码:

<button
       *ngFor="let link of links; let i = index"
       mat-raised-button
       color="primary"
       (click)="selectedIndex = i"
       [routerLink]="link.routerLink">
</button>

我什至不知道这是否可能,但我正在寻找这样的东西:

<button
       *ngFor="let link of links; let i = index"
       mat-raised-button
       (click)="selectedIndex = i"
       [color]="selectedIndex === i ? primary : warm"
       [routerLink]="link.routerLink">
</button>

标签: angularangular-materialmaterial-design

解决方案


很有可能,您需要将字符串文字传递给您的数据绑定[color]

[color]="selectedIndex === i ? 'primary' : 'warm'"

堆栈闪电战

https://stackblitz.com/edit/angular-npzkiu?embed=1&file=app/button-overview-example.html


推荐阅读