0

각도 4의 재료 구성 요소에 사용자 정의 (HEX) 색상을 추가 할 수 있습니까? 이 같은 예를 들어 뭔가 :각도 4 구성 요소의 재료 사용자 정의 색상

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

어디 factor.color입니다 진수 형식의 문자열 (예 : '#CCC'에 대한)

답변

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

내가이 게시물을 사용 : https://stackoverflow.com/a/11508164/5468463 – Vega