salix/front/core/directives/on-error-src.js

23 lines
575 B
JavaScript
Raw Normal View History

2018-02-10 15:18:01 +00:00
import ngModule from '../module';
2021-11-18 13:00:45 +00:00
import noImage from './no-image-dark.png';
2018-01-25 12:37:27 +00:00
/**
* Sets a default image when there is an error loading
* the original image.
*
* @return {Object} The directive
*/
export default function onErrorSrc() {
2018-01-25 12:37:27 +00:00
return {
restrict: 'A',
link: (scope, element, attrs) => {
let imgError = noImage;
2018-01-25 12:37:27 +00:00
element.bind('error', function() {
if (attrs.src != imgError)
2018-01-25 12:37:27 +00:00
attrs.$set('src', imgError);
});
}
};
}
2018-02-10 15:18:01 +00:00
ngModule.directive('onErrorSrc', onErrorSrc);