Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

311
Views
Cómo cambiar limpiamente el estilo de un solo elemento al hacer clic con un color diferente para cada elemento de un ngFor con angular

Estoy comenzando con angular y mi pregunta es bastante simple. (Estoy seguro de que hay una respuesta en Internet, pero no sé cómo formular mi pregunta simplemente)

aquí está mi componente

Solo quiero cambiar el color del botón en el lado derecho de esta manera y cuando hago clic en otro, el primero vuelve a la normalidad y cambia de color .

Aquí está el código de mi componente:

 <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>

Estoy llamando a mi componente de esta manera:

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

Actualmente estoy usando solo algunas instrucciones simples de javascript y el código de mi selectType($event, scrap.color) es:

 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)"; }

¿Hay una manera mejor y más sencilla de hacerlo usando propiedades angulares como ng-class, ng-style u otras?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Parece que su código tiene un javascript antiguo.
Para un mejor enfoque, tal vez Render2 podría ser más útil.
En resumen: Render2 se usa para cambiar el estilo y otras propiedades de un elemento.
Podría crear un componente personalizado que defina el botón.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!