Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 component styles from input

Tags:

We can use interpolation to write an input into the template:

@Component({
    selector: 'tag',
    inputs: ['color'],
    template: `
        <div id="test" style="background: {{color}}">
            Some text
        </div>
    `,  
})
class TestComponent {
}

My question is: Is it possible to get it (somehow) to work like this:

@Component({
    selector: 'tag',
    inputs: ['color'],
    template: `
        <div id="test">
            Some text
        </div>
    `,  
    styles: ['#test { background: {{color}}; }'],
})
class TestComponent {
}

This last attempt does not work, and I cannot seem to find a way how to do it.

Thanks.

like image 797
Willem van Gerven Avatar asked May 07 '16 20:05

Willem van Gerven


1 Answers

AFAIK you can't do that. Component styles metadata wouldn't have access to its Class variable. I'd better suggest you to go for ngClass/ngStyle

<div id="test" [ngStyle]="{ 'background': color }">
    Some text
</div>
like image 70
Pankaj Parkar Avatar answered Sep 28 '22 03:09

Pankaj Parkar