AME
AME

Reputation: 2302

Apply ternary operator inside an expression which uses a filter

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?

Upvotes: 8

Views: 16680

Answers (2)

Billy Moon
Billy Moon

Reputation: 58531

... 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.

Upvotes: 5

Pierre-Alexandre Moller
Pierre-Alexandre Moller

Reputation: 2354

You can do it with parenthesis :

{{ (boolean ? String1 : String2) | weirdoFilter }}

Upvotes: 19

Related Questions