Tampa
Tampa

Reputation: 78294

Angular2 Template syntax - How to do a OR on ngIf

I am trying to loop though a list and want to show and element based on id.

If I do

*ngIf="environment.id!=3" then works. 

cell is hidden. But I want to do

*ngIf="environment.id!=3 OR environment.id!=1" 

which does not work.

<tr *ngFor="let environment of environments">
          <td>{{ environment.name }}</td>
          <td>
              <a *ngIf="environment.id!=3 ? environment.id!=1" [routerLink]="['EditEnvironment', { id: environment.id }]">
                      <i class="icon icon-edit"></i>
              </a>
          </td>
                <td>
            <i (click)="deleteEnvironment(environment)" class="icon icon-cross"></i>
          </td>
        </tr>

Upvotes: 1

Views: 909

Answers (2)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657546

This *ngIf is always true because every value is either != 3 or != 1

 *ngIf="environment.id!=3 OR environment.id!=1" 
  • 2 is != 3 and also != 1
  • 1 is != 3
  • 3 is != 1

With OR only one needs to match.

You might want

     *ngIf="!(environment.id==3 || environment.id==1)" 

Plunker example

Upvotes: 1

Thierry Templier
Thierry Templier

Reputation: 202216

I would try the following:

*ngIf="environment.id!=3 || environment.id!=1" 

Upvotes: 3

Related Questions