1

I'm trying to set the color of all 'button' and 'a' tags to a color coming from a variable. Like this, which obviously doesn't work.

@Component({
styles: [`    
    a, button {
        color: {{ color_from_variable }};
    }
`]
3
  • This is not supported. Use [class.some-class]="..." or [ngClass]="..." on the element or @HostBinding('class.some-class') someClass= true;' Commented Nov 20, 2017 at 16:38
  • @GünterZöchbauer that is only useful when setting a specific class based on a boolean, I need a color set from configuration Commented Nov 20, 2017 at 16:40
  • You can use ngStyle as posted in the answer below. Commented Nov 20, 2017 at 16:42

2 Answers 2

1

This worked for me :

export const mainColor = 'blue';

@Component({
  ...
  styles: [`    
    p {
      color: `+mainColor+`;
    }
 `]
})

Here is a StackBlitz example I made for this : https://stackblitz.com/edit/angular-vpaarz

Sign up to request clarification or add additional context in comments.

Comments

0

You can use ngStyle:

<some-element [ngStyle]="{'font-style': styleExp}">...</some-element>

<some-element [ngStyle]="{'max-width.px': widthExp}">...</some-element>

<some-element [ngStyle]="objExp">...</some-element>

3 Comments

ngStyle would be need to be set to every single a and button element ....
Maybe [ngClass] can help you
I think ngclass only accepts true or false, but I'll check out the documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.