Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use angular material theme color in TypeScript / JavaScript?

I don't know how to get the primary theme color (Angular Material) in a e.g. component.ts.

I can use color="primary in my template.html or mat-color($primary) in a style.scss. But how do I use theme colors for styling an element inside a canvas? How do I use them inside my TypeScript-code?

Info:

"@angular/material": "^5.0.0-rc.2",
"@angular/core": "^5.0.0"
like image 614
Daniel Avatar asked Dec 13 '17 11:12

Daniel


Video Answer


1 Answers

I used a hacky solution. It's not beautiful but working.

component.html

<div #primary class="mat-button mat-primary" style="display:none"></div>

component.ts

declare let getComputedStyle: any;

export class Component implements AfterViewInit {
    @ViewChild('primary') primary: ElementRef;

    ngAfterViewInit() {
       const primaryColor = getComputedStyle(this.primary.nativeElement).color;
    }
like image 123
Daniel Avatar answered Sep 23 '22 11:09

Daniel