角度4材料自定义颜色的组件

时间:2017-10-06 07:01:59

标签: angular typescript material-design angular-material material

是否可以在Angular 4中为材质组件添加自定义(HEX)颜色? 例如:

<div *ngFor="let factor of factors">
   <button md-button color="factor.color">Button</button>
</div>

其中factor.color是十六进制格式的字符串(例如'#CCC')

1 个答案:

答案 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] +')';
}