In Angular.js templating, I need to test if a value is undefined or empty.... I am not sure how to do this since ng-switch-when
tests for expressions in strings. I needed to use ng-switch
because it's a if else condition. Any ideas?
<div ng-switch="vipLabel"> <div ng-switch-when="vipLabel.toString()"> <h1>Getting infomration...one sec</h1> </div> <div ng-switch-default> <h3>Do you wish to unbind {{node.label}} from {{ vipLabel }}?</h3> </div> </div>
$watch() for example, you want to check if the new value is undefined or null, but if that value is a boolean, using your solution will not work.
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.
The [ngSwitch] directive on a container specifies an expression to match against. The expressions to match are provided by ngSwitchCase directives on views within the container. Every view that matches is rendered. If there are no matches, a view with the ngSwitchDefault directive is rendered.
ng-switch allows expressions, you can use placeholder, as such:
<div ng-switch="selection || '_undefined_'" > <span ng-switch-when="_undefined_">I am set to zero or undefined?!</span> <span ng-switch-default>This string is not empty</span> </div>
For more info: ng-switch on empty string
You usually do it within controller(see answer#1 in linked thread).
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