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

711
Visualizações
Change CSS theme in Angular

Im currently working on a Angular2 application with webpack and Im trying to set differents css themes according to the user.

For example : When the user connect, If it's a boy, I want to have my backgrounds blue, and if it's a girl I want the backgrounds to be pink.

Simply changing the css value with setAttribute or style.property wont work because the DOM is destroyed when changing tab in the application, it needs to be kinda permanent.

I've tried using different css stylesheets (1 for each theme) and linking them to my html with javascript when the user connect. Problem is, webpack is always adding automatically my css to my html when building the app.

Thanks for the help.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

In your css, make a rule like :

.is-boy{
    background: blue;
}

.is-girl{
    background: pink;
}

and declare in you angular app a scope var like $scope.userSex = 'boy';

and on your body use ngClass like this

<body [ngClass]="{'is-boy': userSex === 'boy', 'is-girl': userSex === 'girl'}" ...
over 4 years ago · Santiago Trujillo Relatório

0

:host-context selector

You could use the :host-context selector to apply styles to your component based on the parent component.

styles:[`
    :host-context(.parent1) div{
      border: 1px solid blue;
    }

    :host-context(.parent2) div{
      border: 1px solid blue;
    }
  `]

This allows you to conditionally apply styles based on a the selector that wraps the component.

plunker

edit:

So in your case - your parent would have a div with class .boy and a div with class .girl

You could load these containing divs with some flag controlled by ngIf

over 4 years ago · Santiago Trujillo Relatório

0

If you want to be permanent store class value in localStorage. To set the theme use ngClass with variable set to theme you need.

over 4 years ago · Santiago Trujillo 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