Angular Material - 如何在运行时更改按钮颜色

Tho*_*pra 2 material-design angular-material angular

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

我已经阅读了文档: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>
Run Code Online (Sandbox Code Playgroud)

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

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

Mar*_*hal 6

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

[color]="selectedIndex === i ? 'primary' : 'warm'"
Run Code Online (Sandbox Code Playgroud)

闪电战

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