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

162
Visualizações
Why don't my class styles apply to dynamically created elements? Angularjs

My question is simple; why don't my class styles apply to dynamically created elements?

I am creating a search bar here where I generate an li per matching result, and append it to my ul. When I inspect the page, I see the classes are applied to the li's correctly, but the styles from the class itself aren't present. I hard coded a test li and it had the expected styles. What am I missing here in order to have my styles applied to these dynamically generated elements? Surely I don't have to assign every style for the li's in my typescript? Any explanation would be lovely, thank you all! (:

My HTML:

<div class="section">
  <h2>Step 1: Choose an Identity Provider (IDP)</h2>
  <div class="search">
    <input
      class="focusable"
      (focusout)="handleFocusOut()"
      (input)="debounce(search, 300, $event)"
      placeholder="Select Identity Provider"
      autocomplete="off"
    />
    <i class="icon fas fa-search"></i>
    <ul id="search-options">
      <li class="focusable testing">IMG Salesforce</li>
    </ul>
  </div>
  <!--    <i class="fa fa-plus"></i>-->
</div>

My scss:

.section {
...
    .search {
      position: relative;
      width: 300px;
      .icon {
        position: absolute;
        right: 5px;
        top: 3px;
      }
      input {
        width: 300px;
      }
      ul {
        color: red;
        li {
          cursor: pointer;
          &:hover {
            background-color: grey;
            color: red;
          }
          .testing {
              cursor: pointer;
            &:hover {
              background-color: grey;
              color: red;
            }
          }
        }
      }
    }
  }

My TS:

let ul = document.getElementById('search-options');
this.displayServices.forEach((service) => {
  let li = document.createElement('li');
  li.classList.add('focusable', 'testing');
  li.addEventListener('focusout', this.handleFocusOut);
  const img = document.createElement('img');
  img.src = this.getImgUrl(service);
  img.width = 20;
  img.height = 20;
  img.style.margin = '0 10px';

  li.innerHTML = `${service.name}`;
  li.style.display = 'flex';
  li.style.alignItems = 'center';
  li.style.border = '.5px solid black';
  li.style.padding = '8px 0';
  li.prepend(img);
  ul.appendChild(li);
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It's hard to be precise without seeing the whole tamale, but generally you should be getting your data in the .TS file and sending that data directly to the view. Your view should be creating those elements on the fly. Not shown in the answer here is the inline styles you were adding to the image and the LI tag - just do those in CSS.

Something like this:

TS:

this.someService.getData.subscribe(displayServices => {
  this.displayServices = displayServices;
})

HTML:

<div class="section">
    <h2>Step 1: Choose an Identity Provider (IDP)</h2>
    <div class="search">
      <input
        class="focusable"
        (focusout)="handleFocusOut($event)"
        (input)="debounce(search, 300, $event)"
        placeholder="Select Identity Provider"
        autocomplete="off" />
      <i class="icon fas fa-search"></i>
      <ul id="search-options">
        <li *ngFor="service in displayServices" 
             class="focusable testing"
             (focusout)="handleFocusOut($event)">
            <img [src]="getImgUrl(service)" />
            {{service.name}}</li>
      </ul>
    </div>
    <!--    <i class="fa fa-plus"></i>-->
  </div>
about 4 years ago · Juan Pablo Isaza Relatório

0

the classes are applied to the li's correctly, but the styles from the class itself aren't present

If you mean the focusable and testing classes, I don't see them in your SCSS.

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