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

183
Visualizações
Can an Angular 2 component style itself?

In Angular 2 you can include a stylesheet in a component's template. Because of Angular 2's View Encapsulation the styles only apply to the containing elements.

Is there a way to style the component's main tag without putting a surrounding <div class="component-wrapper"></div>?

Example plnkr: http://plnkr.co/edit/rANAsR1h9ERBIZV6wuJ8

It has an external css file adding a border and padding to the <app> tag, and the template attempts to set a background color.

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

0

You have two options

  • Use host property
@Component({
    selector : 'my-component',
    host : {
        '[style.color]' : "'red'", 
        '[style.background-color]' : 'backgroundColor'
    }
})
class MyComponent {
    backgroundColor: string;
    constructor() {
        this.backgroundColor = 'blue';
    }
}
  • Use styles property and :host
@Component({
    selector : 'my-component',
    styles : [`
        :host {
            color: red;
            background-color: blue;
        }
    `]
})
class MyComponent {}

Note there's an odd behavior when using :host and ViewEncapsulation.None.

Here's a plnkr with simple examples for both alternatives.

over 4 years ago · Santiago Trujillo Relatório

0

The Best Simple Way!👇

in your.component.css:

:host {
    background-color: blue;
}
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