Translating a part of the ng-switch documentation :
Please be aware that the values for the [ ng-switch-when
] attribute can not be expressions . Values are interpreted as literals to be compared.
That is, when you use ng-switch-when="condition()"
, the angle is trying to compare the value of the ng-switch
attribute with the condition()
string instead of calling the function.
In your case, I would use ng-show and ng-hide to display an alternative condition to ng-switch
:
<body ng-app ng-controller='controller'>
<label>Type a value: <input type="text" ng-model="value" /></label><br />
<div ng-switch="value" ng-hide="condition()">
<div ng-switch-when="1">1</div>
<div ng-switch-when="3">3</div>
<div ng-switch-when="4">4</div>
<div ng-switch-when="5">5</div>
<div ng-switch-default>None</div>
</div>
<div ng-show="condition()">2</div>
</body>
You can also do this within ng-switch-default
:
<body ng-app ng-controller='controller'>
<label>Type a value: <input type="text" ng-model="value" /></label><br />
<div ng-switch="value">
<div ng-switch-when="1">1</div>
<div ng-switch-when="3">3</div>
<div ng-switch-when="4">4</div>
<div ng-switch-when="5">5</div>
<div ng-switch-default>
<div ng-show="condition()">2</div>
<div ng-hide="condition()">None</div>
</div>
</div>
</body>