是否可以在Angular 4中为材质组件添加自定义(HEX)颜色? 例如:
<div *ngFor="let factor of factors">
<button md-button color="factor.color">Button</button>
</div>
其中factor.color是十六进制格式的字符串(例如'#CCC')
答案 0 :(得分:0)
您可以使用[style.color]属性和一些有关将六角代码转换为RGB的有用帖子:
<强> DEMO 强>
<强> HTML:强>
<button mat-button [style.color]="hexToRgb(factor.color)">Click me</button>
<强>打字稿强>:
....
hexToRgb(hex) {
hex = hex.replace("#",'');
let arrBuff = new ArrayBuffer(4);
let vw = new DataView(arrBuff);
vw.setUint32(0,parseInt(hex, 16),false);
let arrByte = new Uint8Array(arrBuff);
return 'rgb(' + arrByte[1] + "," + arrByte[2] + "," + arrByte[3] +')';
}