Miguel Moura
Miguel Moura

Reputation: 39444

How to define ng-src based on condition?

I have the following:

<img data-ng-src="{{image.Url}}" alt="" />

I need the value in data-ng-src to be different according to a condition.

If image.IsAdvert is true then:

data-ng-src="{{image.Url}}"

if the image.IsAdvert is false then

data-ng-src="file/{{image.Url}}"

How can I do this?

Upvotes: 1

Views: 13704

Answers (2)

squiroid
squiroid

Reputation: 14037

Use conditional operator:-

<img ng-src="{{image.isAdvert?image.Url:('file/' + image.Url)}}"/>

Upvotes: 10

PSL
PSL

Reputation: 123739

You could return it from a function on the scope:

data-ng-src="{{getUrl(image)}}"

and

$scope.getUrl = function(image){
  return image.isAdvert ? image.Url : "file/" + image.Url;
}

Upvotes: 7

Related Questions