realph
realph

Reputation: 4671

Angular: ng-src Fallback

Is there an easy way to fallback from an ng-src if it's returned as null? Currently I'm doing this, but was wondering if there is a better way to write such a conditional:

<td ng-if="m.poster_path == null">
  <img src="default.jpg" />
</td>
<td ng-if="m.poster_path !== null">
  <img ng-src="{{ m.poster_path }}" />
</td>

Any help is appreciated. Thanks in advance!

Upvotes: 0

Views: 292

Answers (1)

Avijit Gupta
Avijit Gupta

Reputation: 5756

As angular evaluates every expression inside {{..}}, the following code would automatically set the img path to 'default.jpg' when m.poster_path is null:

<td>
  <img ng-src="{{ m.poster_path || 'default.jpg' }}" />
</td>

Upvotes: 5

Related Questions