this works:
{{ boolean ? String1 : String2 }}
this doesn't:
{{ boolean ? String1 | weirdoFilter : String2 | weirdoFilter }}
How can I apply filters to ternary expressions ?
edit: Maybe single quotes?
For example, you should not try to make ternary operators nested inside of ternary operators. Although writing code this way works correctly in JavaScript, it is very hard to read and understand.
Example: C Ternary Operatorage >= 18 - test condition that checks if input value is greater or equal to 18. printf("You can vote") - expression1 that is executed if condition is true. printf("You cannot vote") - expression2 that is executed if condition is false.
We can simply use a ternary operator or we can even use a nested ternary operator.
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.
You can do it with parenthesis :
{{ (boolean ? String1 : String2) | weirdoFilter }}
... if you make a function in your view, then it becomes easier to do logic in your controller using real life javascript (instead of more limited angular expressions)...
{{ mySpecificThing(String1,String2) }}
... then in controller ...
$scope.mySpecificThing = function(item1, item2){
return boolean ? $filter('weirdoFilter')(item1) : $filter('weirdoFilter')(item2);
}
As a general pattern, I think it is favourable to always keep your logic away from your templates.
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