2012-09-15 68 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>