UPDATE Below is a custom directive version of this answer. I believe this is the best angular approach. It depends on the service defined in the original answer:
app.directive('imageOrDefault', function (Utils) { return { restrict: 'A', link: function (scope, element, attrs) { console.log(attrs); Utils.isImage(attrs.imageOrDefault).then(function(result) { attrs.$set('src', result); }); } } });
You can use it in your markup as follows:
<img image-or-default="http://r.llb.be/img/search-418.png" height="42" width="42">
Here is a snippet of this version.
Initial answer :
You can use the ng-src directive for this and a service to check if the image really exists. Here is an example of a working plunkr that you can adapt to your exact needs.
There is an isImage() function that checks the URL and returns a promise . If the URL is a valid image path, promise has resolved with that URL. If this is not the case, promise will be resolved with your default image sample.
app.factory('Utils', function($q) { return { isImage: function(src) { var deferred = $q.defer(); var image = new Image(); image.onerror = function() { deferred.resolve('yourDefaultImagePath'); }; image.onload = function() { deferred.resolve(image.src); }; image.src = src; return deferred.promise; } }; });
In the markup, I use ng-src -directive to wait for a resolve promise before setting the src attribute.
<img ng-src="{{result}}" height="42" width="42">
The isImage() function is called from the controller :
app.controller('MainCtrl', function($scope, Utils) { $scope.test = function() { Utils.isImage($scope.source).then(function(result) { console.log(result); $scope.result = result; }); }; });
This solution easily adapts to the user directive. Let me know if you are interested in this approach.
The solution was adapted from: Angular js - isImage () - check if this image is by URL