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

470
Visualizações
Angular 4 CSS on document.createElement()

I try to create elements dynamically in Angular 4.

TS

const bookmark = document.createElement('a');
bookmark.className = 'bookmark';

SCSS

.bookmark {
  display: block;
  background: white;
  color: #999;
  padding: 20px;
  transition: 0.3s ease all;
  border-bottom: 1px solid #DDD;
}

However the style do not apply on the element from the SASS file. If I add the style directly in the JS file, it works:

const bookmark = document.createElement('a');
bookmark.className = 'bookmark';

bookmark.style.display = 'block';
bookmark.style.background = 'white';
bookmark.style.color = '#999';
bookmark.style.padding = '20px';
bookmark.style.transition = '0.3s ease all';
bookmark.style.borderBottom = '1px solid #DDD';

If I create the element directly in the HTML file, it automatically gets a _ngcontent-c1 attribute whilst the one created in TS misses this attribute (if I give it manually in Chrome Developer Tools - Element panel, it gets the style from SCSS).

<div class="bookmarks-list">
  <a class="bookmark">
    I am the bookmark
  </a>
</div>

My question is, how to apply the .bookmark class style from the SASS file to the created bookmark element in TS?

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

0

You didn't share the full code, but probably it's the problem with where you create your element, if it's inside a component, it might not reaching your css due to encapsulation , try to use something like below:

@Component({
// ...
encapsulation: ViewEncapsulation.None, //<<<<< this one!
styles: [
  // ...
]
})
export class HelloComponent {
// ...
}

For more info, visit the link below: https://angular.io/docs/ts/latest/guide/component-styles.html#!#view-encapsulation

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