最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using filters inside a ternary operator in AngularJS - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question asked Jun 15, 2013 at 5:18 kylederkyleder 6671 gold badge8 silver badges18 bronze badges 1
  • 3 To me this looks too plex to be inline. I would create a function that will return the src based on whatever logic you need it to do. – Liviu T. Commented Jun 15, 2013 at 11:21
Add a ment  | 

1 Answer 1

Reset to default 9

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

发布评论

评论列表(0)

  1. 暂无评论