Angular.js ng-switch ---测试值是空还是未定义

dma*_*man 20 angularjs

在Angular.js模板中,我需要测试一个值是未定义的还是空的....我不知道如何ng-switch-when在字符串中测试表达式.我需要使用,ng-switch因为它是一个if else条件.有任何想法吗?

<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>
Run Code Online (Sandbox Code Playgroud)

Ert*_*maa 21

ng-switch允许表达式,你可以使用占位符,如下:

<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>
Run Code Online (Sandbox Code Playgroud)

有关更多信息:ng-switch on empty string

您通常在控制器中执行此操作(请参阅链接线程中的答案#1).


小智 19

<div ng-switch="!!selection">
   <div ng-switch-when="false">Empty, Null or undefined</div>
   <div ng-switch-when="true">The string is not empty</div>
</div>
Run Code Online (Sandbox Code Playgroud)


小智 5

以下解决方案也可以工作(使用角度1.3.10):

<div ng-switch="selection">
    <div ng-switch-default>The string is not empty!</div>
    <div ng-switch-when="undefined">The string is empty!</div>
</div>
Run Code Online (Sandbox Code Playgroud)