For a given js variable, which has style information in it:
menuBgColor = "red";
In AngularJS I was able to use dynamically embedded style:
<div>
<style>
.menuElement{
background-color:{{menuBgColor}}
}
</style>
<div class="menuElement">...</div>
</div>
In Angular2+ I cannot use the same as above. When the view is rendered in the browser variable menuBgColor
is not populated and the element is rendered as coded.
like this -
<body>
...other markup...
<div><style>.menuElement{background-color:{{menuBgColor}}}</style>
...markup...
</body>
I want to know that what are the other ways in angular 2+ to add dynamic styles to the view?
- I know of ngStyle but the given example is a very small one.
- In my case, I have to apply dynamic styles to each element of the whole app like button, borders, background, colors, tree controls, sidebars etc. So ngStyle is very exhaustive.
- I also do not have predefined theme files because theme is managed by a feature in our app where user can define his/her own theme. Here I understand that I can refresh page to get the updated theme BUT consider that there can be 'n' number of users with their own themes.
- So in my use case, the only solution I can think of is that somehow variable interpolate and create an embedded css.
Any help is greatly appreciated.