AWGAL
AWGAL

Reputation: 187

Ngif for empty string validation angular 5

I'm struggling to validate an empty string retrieved from a server

It's usually pretty straight forward it's just not working

<div class="ui-g-2 info-txt"
     *ngIf="appointment.Notes !==null || 
     appointment.Notes !== ''">
     <i class="fa fa-commenting-o" aria-hidden="true"</i>
</div>

<div class="ui-g-5 info-txt"
     *ngIf="appointment.Notes !==null ||     
     appointment.Notes !== ''">
     *emphasized text*{{appointment.Notes}}
</div>

<div class="ui-g-7 info-txt"
     *ngIf="appointment.Notes === null || 
     appointment.Notes === ''"
     style="padding-top: 0;">
     no hay notas disponibles
</div>

So I'm checking for a null or string with nothing in it, the problem is its still showing the icon and the empty string

When there is a message inside the no hay..... doesnt show so its working perfectly on that side

Any help greatly appreciated been stuck on this for ages.

Im sure its a obvious I just cant see it

Upvotes: 3

Views: 12957

Answers (2)

nircraft
nircraft

Reputation: 8478

You can have double negation to check if value is not undefined/null or Empty. Ex: *ngIf="!!name".

This will prevent all Null, undefined or empty values from showing up.

Upvotes: 7

David Anthony Acosta
David Anthony Acosta

Reputation: 4908

Shouldn't it be:

appointment.Notes !==null && appointment.Notes !== ''"

You want it to be not null AND not empty, but you're just checking if it's either not null OR not empty, one or the other. Which means, if you have this value:

const name = "";

The *ngIf condition will be true because it is not null.

Upvotes: 1

Related Questions