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

余生长醉 提交于 2019-12-10 15:12:45

问题


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

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

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

回答1:


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>



回答2:


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>

the syntax for hidden is [hidden]



来源:https://stackoverflow.com/questions/42971883/how-to-hide-html-template-if-a-variable-value-is-undedfined-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!