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

23 lines
570 B
JavaScript
Raw Normal View History

2018-02-10 15:18:01 +00:00
import ngModule from '../module';
import noImage from './no-image.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);