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

170
Visualizações
Angular ngSwitchCase not working as expected

I am using a value called count which is initially set to zero. I have created two buttons to increment and decrement the count value.

<button (click)="increment()">++</button>
<h3 class="count">{{ count }}</h3>
<button (click)="decrement()">--</button>

I want to create another div which displays the count value separately with different colors according the switch conditions.

<div [ngSwitch]="count">
  <h1 style="color: red" *ngSwitchCase="count % 2 == 0">{{ count }}</h1>
  <h1 style="color: green" *ngSwitchCase="count % 3 === 0">{{ count }}</h1>
  <h1 style="color: blue" *ngSwitchCase="count % 7 === 0">{{ count }}</h1>
  <h1 *ngSwitchDefault>{{ count }}</h1>
</div>

The count value is displayed but the color remains black. Can anyone tell me what I am doing wrong? I am new to angular.

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

0

the ngSwitchCase is working as expected. imagine this case:

    switch (this.count) {
      case (this.count % 2 === 0) as any:
        break;
      case (this.count % 3 === 0) as any:
        break;
      case (this.count % 3 === 0) as any:
        break;
      default:
        break;
    }

you would always land in the default case because you are doing a switch on count, and the cases are either true or false

why you don't use *ngIf?

or another solution to your problem cold be this:

<div [ngSwitch]="count">
        <h1 style="color: red"
            *ngSwitchCase="count % 2 === 0 ? count : false">{{ count }}</h1>
        <h1 style="color: green"
            *ngSwitchCase="count % 3 === 0 ? count : false">{{ count }}</h1>
        <h1 style="color: blue"
            *ngSwitchCase="count % 7 === 0 ? count : false">{{ count }}</h1>
        <h1 *ngSwitchDefault>{{ count }}</h1>
    </div>
about 4 years ago · Juan Pablo Isaza Relatório

0

A hack for that, would be something like this:

<div [ngSwitch]="true">
  <h1 style="color: red" *ngSwitchCase="count % 2 == 0">{{ count }}</h1>
  <h1 style="color: green" *ngSwitchCase="count % 3 === 0">{{ count }}</h1>
  <h1 style="color: blue" *ngSwitchCase="count % 7 === 0">{{ count }}</h1>
  <h1 *ngSwitchDefault>{{ count }}</h1>
</div>

But I am sure that we can think of better solutions:

For example, if the count is input, you can calculate the header color like that:

feature.component.ts

@Input() count!: number;

color = '';

ngOnChanges({count}: SimpleChanges) {
  if (count) {
    this.setColor();
  }
}

private setColor() {
  if (this.count % 2 === 0) {
    this.color = 'red';
  }
  ...
  ...
}

Then in the template would be like that:

<div> <h1 style="color: {{ color }}">{{ count }}</h1> </div>
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