Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 871
user1892775 Avatar asked Aug 22 '17 22:08

user1892775


People also ask

How do I know if ngIf is undefined?

How to check if a variable string is empty or undefine or null in Angular. In template HTML component: We can use the ngIf directive to check empty null or undefined. In this example, if stringValue is empty or null, or undefined, It prints the empty message.

How do you check if an HTML is undefined?

You can check the type of the variable: if (typeof(something) != "undefined") ... No need for the parentheses: typeof is an operator, not a function.

What is* in* ngIf in Angular?

Angular expands this into a more explicit version, in which the anchor element is contained in an <ng-template> element. The *ngIf directive is most commonly used to conditionally show an inline template, as seen in the following example. The default else template is blank.

How does* ngIf work?

The ngIf directive removes or recreates a portion of the DOM tree based on an {expression}. If the expression assigned to ngIf evaluates to a false value then the element is removed from the DOM, otherwise a clone of the element is reinserted into the DOM.


1 Answers

You can just write:

*ngIf="testvar !== undefined" 
like image 122
diopside Avatar answered Sep 22 '22 11:09

diopside