Skip to content

Instantly share code, notes, and snippets.

@iambigd
Last active December 10, 2015 17:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iambigd/d8de76fe082e07e9801e to your computer and use it in GitHub Desktop.
Save iambigd/d8de76fe082e07e9801e to your computer and use it in GitHub Desktop.
Angular ngSrc image not found directive
app.directive('errSrc', function() {
return {
link: function(scope, element, attrs) {
scope.$watch(function() {
return attrs['ngSrc'];
}, function (value) {
if (!value) {
element.attr('src', attrs.errSrc);
}
});
element.bind('error', function() {
element.attr('src', attrs.errSrc);
});
}
}
})
<img ng-src="{{getADImageURL(ad)}}" err-src="{{notfound}}" />
angular.module('myApp.controllers').controller('ADCtrl', ['$rootScope', '$scope', '$log', 'API_EVENTS', 'ServerConfig', 'AlertService', '$location', 'blockUI', 'ADService', '$sce', 'dialogs', '$filter',
function($rootScope, $scope, $log, API_EVENTS, ServerConfig, AlertService, $location, blockUI, ADService, $sce, dialogs, $filter) {
$log.log('load ad page');
var notfoundImg = ServerConfig.endpoint + 'app/assets/images/404_img.png';
// alert(notfoundImg);
$scope.notfound = notfoundImg;
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment