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?
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.
You can check the type of the variable: if (typeof(something) != "undefined") ... No need for the parentheses: typeof is an operator, not a function.
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.
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.
You can just write:
*ngIf="testvar !== undefined"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With