我正在使用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) –