Angular.js ng-switch ---测试值是否为空或未定义

21
在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>
3个回答

22

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>

了解更多信息: 空字符串的ng-switch

通常你可以在控制器中完成这个操作(参见链接线程中的答案#1)。


20
<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>

1
轻松是最佳解决方案。 - Derek Adair

5

以下解决方案也可以使用(使用angular 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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接