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

255
Views
¿Cómo puedes tener un IF THEN ELSE en Angular con ngStyle?

Estoy tratando de resaltar filas en una tabla en función de un valor de ESTADO. Puedo hacer que funcione con amarillo o sin color haciendo:

 [ngStyle]="{'background-color': cnresult.STATUS === 1 ? 'yellow' : ''}"

¿Cómo puedo agregar otra opción donde si STATUS === 2 se vuelve rojo?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puede crear un objeto de mapa en el archivo ts

 colorMap = { '1': 'yellow', '2': 'red', } <div [ngStyle]="{'background-color': colorMap[cnresult.STATUS] || ''}"></div>

Con esto puedes agregar múltiples condiciones.

over 4 years ago · Santiago Trujillo Report

0

Puedes hacer algo como esto también,

 [ngStyle] = "{'background-color': getBackground(cnresult.STATUS)}"

Luego, en su archivo component.ts,

 getBackground(status) { (2) switch (status) { case 1: return 'yellow'; case 2: return 'green'; case 3: return 'red'; } }
over 4 years ago · Santiago Trujillo Report

0

Puede encadenar múltiples operaciones ternarias

 [ngStyle]="{'background-color': cnresult.STATUS === 1 ? 'yellow' : cnresult.STATUS === 2 ? 'red' : ''}"

Otra opción, posiblemente más fácil de mantener, sería aplicar condicionalmente la(s) clase(s) así:

 <div [class.yellow]="cnresult.STATUS === 1" [class.red]="cnresult.STATUS === 2" ></div> // This belongs in your .css file .yellow { background-color: yellow; } .red { background-color: red; }
over 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!