2012-09-15 60 views
12

我正在使用Angular,我想用ng-switch来检查字符串是否为空。下面的代码似乎不适合我。空字符串ng开关

<div ng-switch on="foo"> 
    <span ng-switch-when="">This string is empty</span> 
    <span ng-switch-default>This string is not empty</span> 
</div> 

任何帮助,这将不胜感激。

+0

的可能的复制[Angular.js NG-开关---测试值是否为空的或未定义的(http://stackoverflow.com/questions/25453689/angular-js-ng-switch-testing- if-value-is-empty-or-undefined) –

回答

19
<div ng-switch on="foo || 'null'"> 
    <span ng-switch-when="null">This string is empty</span> 
    <span ng-switch-default>This string is not empty</span> 
</div> 
+0

这更有用 – manoj

2

确定重复的Angular.js ng-switch ---testing if value is empty or undefined

接受的答案也不是那么理想。在我那么谦虚的意见中,最优雅和清晰的解决方案在那里的支持率最低。

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