HTML check variable undefined with ngIf excluding case when value is 0

user1892775 picture user1892775 · Aug 23, 2017 · Viewed 41.2k times · Source

I am using Angular 2 and would like to check if a variable value is undefined using the htm *ngIf condition.

If I use <span *ngIf="!testvar">, it also covers the case when variable testvar = 0, but I would like to exclude the case when it is 0.

The following works, but those are 2 checks:

<span *ngIf="!testvar && testvarl != 0"></span>

I would like to check this case with only condition to shorten it and make it cleaner.

Is there a simple way to do this?

Answer

diopside picture diopside · Aug 23, 2017

You can just write:

*ngIf="testvar !== undefined"