3

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

2
  • 2
    Any chance it's undefined? You can try *ngIf="appointment.Notes" which will only be true if Notes is not null, undefined, empty string etc Commented Nov 1, 2018 at 19:17
  • I suggest that you move that logic into a function so that it can be shared and you can also make it more robust without cluttering up the HTML. Like the above user said you can also check for undefined whitespaces ' ' etc Commented Nov 1, 2018 at 19:18

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Legend dude :D . I knew it was something obvious. Thanks alot will accept when the four mins pass
You can also try *ngIf(appointment.Notes) which inturn checks both the conditions if I am not wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.