我正在使用棱 Angular Material 制作侧边栏菜单。我没有找到根据某些组件属性更改按钮颜色的方法。
我已经阅读了文档:https://material.angular.io/components/button/overview
关于主题,它只说:
可以使用 color 属性根据当前主题为按钮着色,以将背景色设置为主要色、重音色或警告色。
这是我的代码:
<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>
最佳答案
很有可能,您需要将字符串文字传递给数据绑定(bind) [color]
[color]="selectedIndex === i ? 'primary' : 'warm'"
堆栈 Blitz
https://stackblitz.com/edit/angular-npzkiu?embed=1&file=app/button-overview-example.html
关于Angular Material - 如何在运行时更改按钮颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54117847/