Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

246
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda