0

I have this code to only show the weekly hour if the value is not undefined. However, this ngIf check still show undefined on the element. I already tried *ngIf="item.weeklyHour !== undefined" as well but the undefined still shows up. What am I missing?

<span class="c-f__weekly-hour" *ngIf="item.weeklyHour">
    {{item.weeklyHour}}
</span>
3
  • 2
    I guess item.weeklyHour is not undefined but "undefined" (You (accidententaly ?) converted it to string somewhere). That would explain this behavior... Commented Jul 26, 2017 at 7:38
  • use <span class="c-f__weekly-hour" *ngIf="item && item.weeklyHour"> Commented Jul 26, 2017 at 7:44
  • Your question is confusing: "show the weekly hour if the value is not undefined" and " the undefined still shows up" Commented Jul 26, 2017 at 7:48

2 Answers 2

2

Use ? operator

<span class="c-f__weekly-hour" *ngIf="item?.weeklyHour">

Here is the PLunker ,

please open the app.ts file and review the output by commenting and uncommenting the item object.

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

3 Comments

Have you tried this? AFAIR safe-nav operator doesn't work with ngIf.
will you please post the item json object?
Use this <span class="c-f__weekly-hour" [hidden]="!item?.weeklyHour">
1

I found the answer! It's combination of @Vivek & @n00dl3! The json contains not only the value but also a string so the value was undefined blabla. So I updated the json and also use the *ngIf="item?.weeklyHour".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.