0

I would like to modify quite a large amount of styles on a page through a customisable panel. When a user clicks an option, the content on the page will completely change based on whatever was clicked.

This cannot be a scenario where a class is appended to a parent element and use CSS/LESS to adjust accordingly. For this scenario (for requirement reasons) the CSS needs to be internal on the angular component HTML.

Is it possible to have a value in the component TS like this:

myNewColour: "red"

That can then be used in an internal style sheet that's inside my angular component.html like this?:

<style>
  .myContainer { background: myNewColour }
</style>

<!-- HTML Content -->
<div class="myContainer"> Stuff </div>

Any help with this would be appreciated! :)

2
  • Yes you can use ngStyle or ngClass . Explained in my recent answer stackoverflow.com/questions/47402693/… Commented Nov 21, 2017 at 13:55
  • Thank @mohituprim - However, this needs to be a scenario where ideally the internal style (not inline style) is updated. I'll have a huge amount of data that will change look wise and cannot apply everything inline to the HTML element. Commented Nov 21, 2017 at 14:03

1 Answer 1

1

"Internal in the HTML template" is called inline style ;) Apart from that, you can use ngStyle like so

<tag [ngStyle]="{'background': myNewColour}"></tag>

EDIT if it makes your code too long, what you can do is simply

let customStyle = {
  'background': this.myNewColour
};

And in your tag, simply

<tag [ngStyle]="customStyle"></tag>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Trichetriche. Inline is styles applied to the HTML element. Internal is a style tag inside the component that encompasses all styles. That's primarily my question. Is it possible to dynamically update any values inside the style tag within the angular component. What you have suggested is what I'm already doing and it's proving quite difficult to maintain with the amount of elements I need to update :) Thanks for taking the time to respond though. Much appreciated.
If your problem is how to maintain the code that is too long, see my edit !

Your Answer

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