I'm using Angular and I want to check when a string is empty using ng-switch. The following code doesn't seem to be working for me.
<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>
Any help with this would be appreciated.
NgSwitchCaselinkProvides a switch case expression to match against an enclosing ngSwitch expression. When the expressions match, the given NgSwitchCase template is rendered. If multiple match expressions match the switch expression value, all of them are displayed.
The ng-switch directive lets you hide/show HTML elements depending on an expression. Child elements with the ng-switch-when directive will be displayed if it gets a match, otherwise the element, and its children will be removed.
AngularJS is built on top of JavaScript and it has no different syntax for switch case than JavaScript(As long as you are using it in script).
The ngSwitch directive is used to conditionally swap DOM structure on your template based on a scope expression.
<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>
You will need to include the empty string in your controller:
function Ctrl($scope) {
$scope.items = ['', 'other'];
$scope.selection = $scope.items[0];
}
For additional reference, see the API: http://docs.angularjs.org/api/ng.directive:ngSwitch
Also, here's a working version of your code: http://jsfiddle.net/upPgU/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With