Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

242
Vistas
Angular: How to dynamically set colors to elements using a color input and save them to a database

I am trying to set CSS colors and styles that is fetched from an API endpoint to HTML elements.

I have been able to achieve this but I feel it is not the best practice.

This is what I have. Any suggestion to achieve this in a more professional way?

my .ts file:

getColors(){
    this.api.get("http://127.0.0.1:8000/colors").subscribe((res:any)=>{
      this.colors = res.color
      console.log(res)
      document.documentElement.style.setProperty("--primary", this.colors)
    })
  }

and .html file:

<header class="header">
    <div class="headerdiv">
        <h1>Welcome to Overflow</h1>
    </div>
</header>



<style>
    .header{
        background-color: var(--primary);
    }
    h3, h1{
        color: var(--h3color);
    }
</style>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You should not inject CSS or any code via endpoint.

Loading CSS is problamtic. You are making you app very vunarable to code injection.

See Topics like CSS injection: what's the worst that can happen?

I suggest rethink you approach and load the color from inside the app. Like getting a id from the api which translates to a color. In that way you can control the color from outside without injection potential malicious code.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda