Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 693
forgottofly Avatar asked Jan 08 '23 15:01

forgottofly


2 Answers

This should work:

<div ng-src="{{imageName ? imageUrl : 'img/avatar.png'}}">
like image 70
Bazinga Avatar answered Jan 16 '23 10:01

Bazinga


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.

like image 30
Farid Suryanto Avatar answered Jan 16 '23 09:01

Farid Suryanto