Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular.js ng-switch ---testing if value is empty or undefined

Tags:

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> 
like image 785
dman Avatar asked Aug 22 '14 18:08

dman


People also ask

How check variable is empty or not in Angularjs?

$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.

Is empty in angular?

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.

What is ngSwitch?

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.


1 Answers

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).

like image 119
Erti-Chris Eelmaa Avatar answered Oct 03 '22 21:10

Erti-Chris Eelmaa