Can I somehow use if-then-else construction (ternary-operator) in angularjs expression, for example I have function $scope.isExists(item) that has to return bool value. I want something like this,
<div ng-repeater="item in items">
<div>{{item.description}}</div>
<div>{{isExists(item) ? 'available' : 'oh no, you don't have it'}}</div>
</div>
I know that I can use function that returns string, I'm interesting in possibility of using if-then-else construction into expression. Thanks.
You can easily use ng-show such as :
For more complex tests, you can use ng-switch statements :
Angular expressions do not support the ternary operator before 1.1.5, but it can be emulated like this:
So in example, something like this would work:
UPDATE: Angular 1.1.5 added support for ternary operators:
You can use ternary operator since version 1.1.5 and above like demonstrated in this little plunker (example in 1.1.5):
For history reasons (maybe plnkr.co get down for some reason in the future) here is the main code of my example: