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

253
Visualizações
How can you have an IF THEN ELSE in Angular with ngStyle?

I am trying to have rows in a table highlight based on a STATUS value. I can get it working with either yellow or no color by doing:

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

How can I add another option where if STATUS === 2 it goes to red?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can create a map object in the ts file

colorMap = {
    '1': 'yellow',
    '2': 'red',
}

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

By this you can add multiple conditions

over 4 years ago · Santiago Trujillo Relatório

0

You can do something like this as well,

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

Then in your component.ts file,

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

0

You can chain multiple ternary operations

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

Another, possibly more maintainable option would be to conditionally apply class(es) like so:

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