How to hide HTML template if variable value is undedfined / null?

I have used ngFor to display my data. And I want to hide the div if the variable value from ngFor is empty / undefined. My code is below. Can anyone help.

<li *ngFor="let parcel of dataSource;">

   <span hidden="parcel.ID==''">{{parcel.refrence }}</span>
</li>

      

+3


source to share


2 answers


Use *ngIf

since your parcelID is undefined your condition should be*ngIf="parcel.ID"



<li *ngFor="let parcel of dataSource;">
   <span *ngIf="parcel.ID">{{parcel.refrence }}</span>
</li>

      

+4


source


use this code -

<li *ngFor="let parcel of dataSource;">
   <span [hidden]="parcel.ID !== ''">{{parcel.refrence }}</span>
</li>

      

or



<li *ngFor="let parcel of dataSource;">
   <span *ngIf="parcel.ID == ''">{{parcel.refrence }}</span>
</li>

      

syntax for hidden [hidden]

0


source







All Articles