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

106
Views
¿Cómo puedo poner una propiedad de objeto dentro de ngClass?

Tengo el siguiente código:

 <td *ngFor="let prop of props" [ngClass] = "{ componentClasses[prop]: true }" > Something </td>

Las clases de componentes se ven así:

 componentClasses = {'id': 'class1', 'name': 'class2', 'salary': 'class3'};

this.props se ve así:

 this.props = ['id', 'name', 'salary'];

¿Por qué quiero hacer esto? Para que no codifique cada clase. En su lugar, lo automatizaría, lo cual es útil si tiene una tabla grande con diferentes propiedades css.

El problema es:

Esta sintaxis no funciona. A ngClass no parece gustarle este tipo de estructura dentro de object[prop]: .

Probé el siguiente código también, pero sin ningún éxito.
'`$(componentClasses[prop])$`': verdadero

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

en html usamos accesorios solo no this.props

 <td *ngFor="let prop of props" [ngClass] = "{ componentClasses[prop]: true }" > Something </td>
about 4 years ago · Santiago Trujillo Report

0

moverlo a los ts? Además, usar ngFor dará como resultado múltiples td

 <td [ngClass] = "classNames" > Something </td>
 componentClasses = {'id': 'class1', 'name': 'class2', 'salary': 'class3'} props = ['id', 'name', 'salary'] get classNames(){ return this.props.reduce((acc,key)=>acc[componentClasses[key]]=true,{}) }

o

 componentClasses = {'id': 'class1', 'name': 'class2', 'salary': 'class3'} props = ['id', 'name', 'salary'] get classNames(){ return this.props.map((key)=>componentClasses[key]) }
about 4 years ago · Santiago Trujillo Report

0

tienes un código [ngClass]="componentClasses[prop]"

ACTUALIZAR: pasarlo como una matriz:

 [ngClass]="[componentClasses[prop], componentClasses[prop] + '1' ]"
about 4 years ago · Santiago Trujillo 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!