-
Notifications
You must be signed in to change notification settings - Fork 1
/
ng-image-fallback.js
46 lines (42 loc) · 1.74 KB
/
ng-image-fallback.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
(function () {
'use strict';
angular
.module('ng-image-fallback',[])
.directive('imageFallback', imageFallback);
imageFallback.$inject = ['$rootScope', '$timeout'];
/* @ngInject */
function imageFallback($rootScope, $timeout) {
var directive = {
link: link
};
return directive;
function link(scope, element, attrs, controller) {
$(element).attr('src', attrs.loadingImage)
attrs.$observe('ngSrc', function () {
$timeout(function () {
if (angular.isDefined(attrs.ngSrc)) {
if (attrs.ngSrc.length > 0) {
//$(element).attr('src', attrs.loadingImage)
var img = new Image();
img.src = attrs.ngSrc;
$(img).imagesLoaded()
.always(function (instance) {
})
.done(function (instance,image) {
$(element).attr('src',image.img.src)
})
.fail(function () {
console.log('error')
$(element).attr('src', attrs.errorImage)
})
.progress(function (instance, image) {
var result = image.isLoaded ? 'loaded' : 'broken';
console.log('image is ' + result + ' for ' + image.img.src);
});
}
}
})
})
}
}
})();