forgottofly
forgottofly

Reputation: 2719

Replace with default image if image not found -angularjs

What could be the best possible condition to check and display if the image path is undefined in AngularJS. I have tried like this:

HTML:
<div ng-src="{{imageUrl}}" == "null" || src="img/avatar.png" >

CONTROLLER:
$scope.imageUrl="125.178.1.127/uploads/image" +$scope.imageName;

If $scope.imageName is undefined I have to load/show the default image img/avatar.png

Upvotes: 4

Views: 7173

Answers (2)

Farid Suryanto
Farid Suryanto

Reputation: 11

I use the Angular Image Fallback modul and perfectly works! Just place fallback-src and loading-src to the image tag.

In your modul:

angular.module('starter', ['dcbImgFallback'])

In your HTML:

<img ng-src="your image path" fallback-src="your image default" loading-src="your image loading">

This is also works in ng-repeat. Hope this can helps.

Upvotes: 1

Bazinga
Bazinga

Reputation: 11244

This should work:

<div ng-src="{{imageName ? imageUrl : 'img/avatar.png'}}">

Upvotes: 8

Related Questions