dman dman - 6 months ago 42
AngularJS Question

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

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>

Answer

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

Comments