Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using filters inside a ternary operator in AngularJS

Is there a method for applying a filter to a variable in the template when it is part of a ternary operation?

<img ng-src="{{ image_url && image_url|filter:"foo" || other_url }}">

In this case the filter is a custom filter, but one that I don't want to modify to handle the ternary operation (because the filter maybe different depending on where it's used and I don't want to reimplement that logic a bunch of times).

like image 749
kyleder Avatar asked Jan 14 '23 03:01

kyleder


1 Answers

Liviu T. is probably right in most cases: you'd want to create a function on the scope that returns the right data for you in this case.

That said, you can get by by wrapping the filtered expression in parens:

image_url && (image_url | filter:"foo") || other_url

Fiddle

like image 152
satchmorun Avatar answered Jan 22 '23 01:01

satchmorun