ng-switch on empty string

Tim*_*wen 14 javascript angularjs

我正在使用Angular,我想使用ng-switch检查字符串是否为空.以下代码似乎对我不起作用.

<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>
Run Code Online (Sandbox Code Playgroud)

任何帮助都将不胜感激.

chr*_*ina 21

<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>
Run Code Online (Sandbox Code Playgroud)


小智 10

您需要在控制器中包含空字符串:

function Ctrl($scope) {
  $scope.items = ['', 'other'];
  $scope.selection = $scope.items[0];
}
Run Code Online (Sandbox Code Playgroud)

有关其他参考,请参阅API:http://docs.angularjs.org/api/ng.directive:ngSwitch

此外,这是您的代码的工作版本:http://jsfiddle.net/upPgU/