angular - 使用 [ngStyle] 将自定义颜色应用于 mat-raised-button
问题描述
我想使用自定义垫按钮颜色样式,但是当我将它与 [ngStyle] 结合使用时它不起作用。
这就是我要的:
html:
<button mat-raised-button
[ngStyle]="getDirectionArrowStyle(item.label, 'ltr')"
value="ltr"
aria-label="Left To Right"
(click)="setActiveDirection(item.label, 'ltr')">
<mat-icon>east</mat-icon>
</button>
CSS:
.mat-arrow-selected {
background-color: green;
color: #fff;
}
.mat-arrow-deselected {
background-color: red;
color: #fff;
}
转换工作正常,但颜色不是。
打字稿:
getDirectionArrowStyle(label: string, directionValue: string) {
const isActiveDirection = this.isActiveDirection(label, directionValue);
const styles = {
color: isActiveDirection ? 'arrow-selected' : 'arrow-deselected',
transform: isActiveDirection ? 'scale(1.2)' : 'scale(0.5)'
};
return styles;
}
下面的 html 可以正常工作,因此自定义 mat-button 颜色可以正常工作。
html:
<button mat-raised-button
color="arrow-selected"
value="ltr"
aria-label="Left To Right"
(click)="setActiveDirection(item.label, 'ltr')">
<mat-icon>east</mat-icon>
</button>
我可以使用 *ngIf="isActiveDirection(label, directionValue)" 并有两个按钮元素。一个颜色=“箭头选择”,另一个颜色=“箭头取消选择”,但更清洁的解决方案是通过[ngStyle]。因此,如果可以的话,我会非常高兴。
欢迎所有帮助或提示。
解决方案
我想您的主题中有那些颜色名称?该color
属性对于材质控件是特殊的。它不是 CSS。ngStyle
返回 CSS。
尝试:
<button mat-raised-button
[ngStyle]="getDirectionArrowStyle(item.label, 'ltr')"
[color]="getDirectionColour(item.label, 'ltr')"
value="ltr"
aria-label="Left To Right"
(click)="setActiveDirection(item.label, 'ltr')">
<mat-icon>east</mat-icon>
</button>
您需要另一个函数来获取颜色名称:
getDirectionColour(label: string, directionValue: string) {
const isActiveDirection = this.isActiveDirection(label, directionValue);
return isActiveDirection ? 'arrow-selected' : 'arrow-deselected';
}
推荐阅读
- mysql-8.0 - 在 innodb 集群中获取 ipallowlist
- python - 同时基于2列在python中删除数据集中的重复项
- github-api - 将资产上传到版本需要 Github 应用权限
- android - onActivityResult() 在 Android 上不起作用
- clang - CLang AST 如何获得宏连接的 CharRange?
- python - 加快应用函数到 200 万行的 DataFrame
- javascript - 如何从 React 组件中重构异步函数
- android - 将 Android Studio 更新到北极狐版本并将 gradle 升级到 7.0.0 后,我收到 gradle 错误
- python - 如何从 tiktok 网站获取关注者数量?
- reactjs - 使用带有“放大更新身份验证”的 aws-amplify-react 添加新的注册所需属性