A
A
Alex102142022-03-26 16:34:10
Angular
Alex10214, 2022-03-26 16:34:10

How to set data in css variable that came from the back?

Hello, I have data for a dark site background. They are stored in the MSQL database (The colors just lie there). I need to take these colors from the base and substitute variables in CSS. I took the colors, but I don’t know how to further substitute them in CSS variables. Tell me how to do it? And is it possible to implement this at all?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2022-04-05
@Alex10214

Shouldn't be a problem

export class Component {
  constructor(
    private _http: HttpClient,
    private _elementRef: ElementRef
  ) {}

  ngOnInit() {
    const json = '{"--background-color": "black", "--text-color": "red"}';
    this._http.get('host')._subscribe(_res => {
      for (let [key, value] of Object.entries(
        JSON.parse(json)
      )) {
        this._elementRef.nativeElement.style.setProperty(
          key,
          value
        );
        console.log({ key, value });
      }
    });
  }
}

  • https://tyapk.ru/blog/post/angular-theming-with-cs...
  • https://netbasal.com/binding-css-variables-in-angu...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question