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

javascript - Showing image using ternary operator with AngularJs Data Binding - Stack Overflow

programmeradmin5浏览0评论

I am trying to use angular.js ternary operator to show an image, I was wondering if there was a way to achieve something like the following

{{enquiry.buyer_email ? <img ng-src="/images/ico_yes.png"> : <img ng-src="/images/ico_no.png"> }}

I am trying to use angular.js ternary operator to show an image, I was wondering if there was a way to achieve something like the following

{{enquiry.buyer_email ? <img ng-src="/images/ico_yes.png"> : <img ng-src="/images/ico_no.png"> }}
Share Improve this question edited May 5, 2016 at 1:52 seus asked May 5, 2016 at 1:41 seusseus 5689 silver badges32 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

I'm assuming you have a mistake in your paths because they are the same, so I'm going to use ico_no.png instead for one of the cases.

You could do something like this with ternary operators (Angular >= 1.1.5):

<img ng-src="{{enquiry.buyer_email ? '/images/ico_yes.png' : '/images/ico_no.png'}}">

Or something like this with binary operators:

<img ng-src="{{enquiry.buyer_email && '/images/ico_yes.png' || '/images/ico_no.png'}}">

or you could use ng-show:

<img ng-src="/images/ico_yes.png" ng-show="enquiry.buyer_email">
<img ng-src="/images/ico_no.png" ng-show="!enquiry.buyer_email">
发布评论

评论列表(0)

  1. 暂无评论