We are making an Angular2 application and we want to be able to somehow create a global CSS variable (and update the properties' values whenever changed when the variable is assigned).
We had used Polymer for a while (now we are switching to Angular2 components) and we had used CSS properties (Polymer has some polyfill) and we had just update styles using Polymer.updateStyles()
.
Is there any way how we can achieve a similar function?
EDIT:
We want something similar to Sass color: $g-main-color
or to CSS custom properties color: var(--g-main-color)
and whenever we decide to change the value of the property, e.g. something like updateVariable('g-main-color', '#112a4f')
it dynamicly update the value everywhere. All that while an app is running.
EDIT 2:
I want to use some global CSS variables in different parts (host, child element...) of my CSS and be able to change the value immediately - so if I change my-color variable, it changes everywhere in app.
I will use Sass syntax for example:
:host { border: 2px solid $my-color }
:host .some-label { color: $my-color }
Is possible to use something like Angular pipes? (But it supposedly only works in HTML)
:host { border: 2px solid {{ 'my-color' | cssvariable }} }
:host .some-label { color: {{ 'my-color' | cssvariable }} }
1) Using inline styles
2) Use multiple CSS classes mapping to what you want and switch classes like:
Code samples from: https://angular.io/cheatsheet
More info on ngClass directive : https://angular.io/docs/ts/latest/api/common/index/NgClass-directive.html
You don't have any example code but I assume you want to do something like this?
You assign
ng-class
to a variable which is dynamic (a property of a model calledTodoModel
as you can guess).todo.toggle()
is changing the value oftodo.status
and there for the class of the input is changing.This is an example for class name but actually you could do the same think for css properties.
I hope this is what you meant.
This example is taken for the great egghead tutorial here.
I did this plunker to explore one way to do what you want.
Here I get
mystyle
from the parent component but you can get it from a service.Angular 6 + Alyle UI
With Alyle UI you can change the styles dynamically
Here a demo stackblitz
Html
For change Style
Github Repository
Just use standard CSS variables:
Your global css (eg: styles.css)
In your component's css o whatever it is:
Then you can change the value of the variable directly with TS/JS by setting inline style to html element:
Otherwise you can use jQuery for it: