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

2

2 Answers 2

8

This should work:

<div ng-src="{{imageName ? imageUrl : 'img/avatar.png'}}">
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.