{{ info.ID }} | Renders: {{ info.ID }} | Renders: {{ info.ID }} | Renders:

Angular ng-show Still Shows <div> Element When Empty

1k Views Asked by At

How can I hide/remove the containing <div> element when the value is empty:

<div class="small" ng-show="ID !== ''">{{ info.ID }} | </div>

Renders:

<div class="small">|</div>

Can I remove the <div> completely if empty? I've tried:

<div class="small" ng-show="!ID">{{ info.ID }}</div >
2

There are 2 best solutions below

1
Pranav C Balan On BEST ANSWER

You are checking value of ID property which is not the ID within info object so use info.ID within the ng-show.

<div class="small" ng-show="info.ID">{{ info.ID }} | </div>
<!-- -----------------------^^^^^^^----------------------->

If you don't want to render the element itself then use ng-if directive since ng-show directive simply hide using some CSS.

<div class="small" ng-if="info.ID">{{ info.ID }} | </div>
<!-- ---------------------^^^^^^^----------------------->
0
Udi Mazor On

If you only want to hide the element then use:

<div class="small" [hidden]="info?.ID">{{ info?.ID }}</div>

If you alse want to avoid rendering it (which is better in most cases) then use:

<div class="small" *ngIf="info?.ID">{{ info?.ID }}</div>

Use the Elvis operator otherwise you may get this mistake:

Can't get ID of null