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

314
Visualizações
How to cleanly change the style of a single element on click with a different color for each element of an ngFor with angular

i'm starting with angular and my question is quite simple. (I'm sure there is an answer on the internet but I don't know how to formulate my question simply)

here is my component

I just want to change the color of the button on the right side like this and when i click on another one the first one get back to normal and the actual change color

Here is the code of my component :

<div class="container-scrap">
<div class="legend">
    <div class="color-sample" [ngStyle]="{'background-color': scrap.color}"></div>
    <p>{{scrap.scrapName}}</p>
</div> 
<div>
    <button (click)="selectType($event, scrap.color)" class="legend-button" [disabled]="!availableInput"></button>
</div> 

I'm calling my component this way :

<app-scrap-list *ngFor="let scrap of scrapList" [scrap]="scrap" [availableInput]="availableInput"></app-scrap-list>

Currently i'm using only some simple javascript instruction and the code of my selectType($event, scrap.color) is :

  selectType({target} : any, color : string): void {
    this.scrapListService.setSelected(this.scrap.id);

    //(document.querySelectorAll(".legend-button") as HTMLCollectionOf<HTMLElement>).forEach(btn => btn.style.backgroundColor = "white")
    const legendButton = Array.from(
      document.getElementsByClassName('legend-button') as HTMLCollectionOf<HTMLElement>
    );
    
    legendButton.forEach(btn => {
      btn.style.backgroundColor = 'whitesmoke';
      btn.style.borderColor = 'grey';
    });
    

    target.style.backgroundColor = color;
    target.style.borderColor = color;
    target.style.backgroundImage = "url(../../assets/ajout_rectangle_active.png)";
  }

Is there a better and simple way to do it using angular properties like ng-class, ng-style or others?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your code looks like have some old fashoned javascript.
For a better approch, maybe Render2 could be more useful.
In short: Render2 is used to change style and other properties to a element.
You could create a custom component that defines the button.

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