9

How can I use component variables within style TAG in Angular 2?

I do have an Angular 2 component for my header which I like to color depending on a users setting. Thus I'd like to assign an background and font color. While I know how to to this with an attribute binding to an element, I couldn't figure out how to use in a style tag.

Using attribute binding for style works well, however this gets pretty anoying for several subelements, especially if they are nested within other sub components. [ngStyle]= attribute is also only working on a single element.

<header id="header" [style.background-color]="changeBackground()">
  <div>
    Some Text
    <a href="asdf">Some Link</a>
    <subcomponent></subcomponent>
  </div>
  <div> ... a lot mor stuff </div>
</header>

Thus I'd like to add something like

<style>
#header, #header a {
  color: {{mycolor}};
}
</style>

to the html template. However this is not working

Similar Questions do not answer this question yet and only show attribute binding as a solution:

3
  • 1
    #header, #header a { is a CSS selector that selects the element with the id header and it's child <a> element. I wouldn't expect template variable to work within style tags. You should rather use ngStyle. Also binding to methods in the view ="changeBackground()" is discouraged because it is called for every change detection cycle. Rather assign the result to a property and bind the view to the property instead. Commented Nov 26, 2016 at 12:42
  • Possible duplicate of Angular2 dynamic change CSS property Commented Feb 7, 2017 at 17:07
  • I know this question is a bit old now, but this answer might help you stackoverflow.com/a/48265526/1389807 Commented Nov 13, 2019 at 20:43

3 Answers 3

1

It looks to me like you are just creating a new component called 'subcomponent', why not do that?

subcomponent.ts:

import { Component } from '@angular/core';

@Component({
  selector: 'subcomponent',
  templateUrl: './subcomponent.html',
})
export class SubComponent {
  mycolor = 'blue';
}

subcomponent.html:

<style>
#header, #header a {
  color: {{mycolor}};
}
</style>
Sign up to request clarification or add additional context in comments.

11 Comments

Was my first thought too, but this is not working either. Angular extracts the style tag, inserts it into head and does not replace mustache variables in this part. :(
Check out this SO question: stackoverflow.com/questions/2830296, it may answer why angular is moving the style tag and how to work around it.
however it still does not replace the mustache variable in my case :/
@Manuel What issues are you still facing? Are you referring to the {{mycolor}} interpolation?
I'm experiencing this problem today. Can't believe there is still no way around it.
|
-2

To your @Component object, add

styles:[ `#header, #header a {
  color: {{mycolor}};
}`]

So for example:

@Component({
  template: `<header id="header" [style.background-color]="changeBackground()">
    <div>
      Some Text
      <a href="asdf">Some Link</a>
      <subcomponent></subcomponent>
    </div>
   <div> ... a lot mor stuff </div>
  </header>`,
  styles: [ `#header, #header a {
    color: {{mycolor}};
    }`
  `]
})

1 Comment

This is not working. It write mustache code into css which results in an invalid css property value #header[_ngcontent-mpf-4] { background-color: {{currentPartner.partnerColorPrimaryBackground}} !important; }
-3

Use NgStyle as explained in this answer

https://stackoverflow.com/a/41903349

in short

<header id="header" [ngStyle]="getStyle()">
  <div>
    Some Text
    <a href="asdf">Some Link</a>
    <subcomponent></subcomponent>
  </div>
  <div> ... a lot mor stuff </div>
</header>

and

getStyle(): any {
    return {"background-color" : this.changeBackgroundColor()};
}

1 Comment

Read the question. They do not want to use binding.

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.