4

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

forgottofly
  • 2,729
  • 11
  • 51
  • 93
  • possible duplicate of [if a ngSrc path resolves to a 404, is there a way to fallback to a default?](http://stackoverflow.com/questions/16310298/if-a-ngsrc-path-resolves-to-a-404-is-there-a-way-to-fallback-to-a-default) – Josh C. Mar 30 '15 at 12:53
  • But my condition dosen't resolve to a 404.It gets undefined instead – forgottofly Mar 30 '15 at 12:54

2 Answers2

8

This should work:

<div ng-src="{{imageName ? imageUrl : 'img/avatar.png'}}">
Bazinga
  • 10,716
  • 6
  • 38
  • 63
1

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.