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

293
Visualizações
if else in angular2 template syntax

Angular2..why and how?

How do I execute the below if condition in angular2

    <td *ngFor="let val of rows;let j=index">
            IF J==0
                 <label>{{val}}</label>
            ELSE:
               <label style="color:#000000;font-size:12px;padding-top: 5px">{{val}}</label>

</td>
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can use the *ngIf structural directive with the if-else syntax to achieve this result.

<label *ngIf="j === 0; else elseBlock">{{val}}</label>
<ng-template #elseBlock>
    <label style="color:#000000;font-size:12px;padding-top: 5px">{{val}}</label>
</ng-template>

Another option is to use two *ngIf blocks and invert the conditional, like so:

<label *ngIf="j === 0">{{val}}</label>
<label *ngIf="j !== 0" style="color:#000000;font-size:12px;padding-top:5px">{{val}}</label>
about 4 years ago · Santiago Trujillo Relatório

0

If you plan on upgrading to Angular 4, you could use the new if / else in template that includes this version. Example:

<div *ngIf="someCondition; else falsyTemplate">
  <h1>Condition Passed!</h1>
</div>

<ng-template #falsyTemplate>
  <h1>Condition Failed!</h1>
</ng-template>

Check the following useful links:

  • http://angularjs.blogspot.it/2017/03/angular-400-now-available.html
  • http://tylerscode.com/2017/03/angular-4-ngifelse/
  • https://medium.com/aviabird/ngif-else-lands-in-angular-2-0-a242940e54ff

I would personally recommend upgrading to Angular 4.

about 4 years ago · Santiago Trujillo Relatório

0

You can do this in Angular 2 by using the <ng-container> element. In your example it should be something like this:

<td *ngFor="let val of rows;let j=index">
    <ng-container *ngIf="J==0">
        <label>{{val}}</label>
    </ng-container>
    <ng-container *ngIf="J!=0">
        <label style="color:#000000;font-size:12px;padding-top: 5px">{{val}}
</label>
    </ng-container>
</td>

You can find more information in the docs: https://angular.io/guide/structural-directives#ng-container-to-the-rescue.

about 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