我正在使用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>
任何幫助,這將不勝感激。
我正在使用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>
任何幫助,這將不勝感激。
您需要在您的控制器空字符串:
function Ctrl($scope) {
$scope.items = ['', 'other'];
$scope.selection = $scope.items[0];
}
有關更多內容,請參考API:http://docs.angularjs.org/api/ng.directive:ngSwitch
而且,這裏是你的代碼的工作版本:http://jsfiddle.net/upPgU/
一個基本用例需要一個解決方法:(。 – Den
<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>
這更有用 – manoj
確定重複的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>
的可能的複製[Angular.js NG-開關---測試值是否爲空的或未定義的(http://stackoverflow.com/questions/25453689/angular-js-ng-switch-testing- if-value-is-empty-or-undefined) –