This repository has been archived by the owner on Mar 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
53 lines (47 loc) · 1.48 KB
/
index.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
47
48
49
50
51
52
53
/*global angular, Blob, URL */
(function () {
'use strict';
angular.module('angular.img', [])
.directive('httpSrc', ['$http', function ($http) {
return {
// do not share scope with sibling img tags and parent
// (prevent show same images on img tag)
scope: {
httpSrc: '@'
},
link: function ($scope, elem, attrs) {
function revokeObjectURL() {
if ($scope.objectURL) {
URL.revokeObjectURL($scope.objectURL);
}
}
$scope.$watch('objectURL', function (objectURL) {
elem.attr('src', objectURL);
});
$scope.$on('$destroy', function () {
revokeObjectURL();
});
attrs.$observe('httpSrc', function (url) {
revokeObjectURL();
if (url && url.indexOf('data:') === 0) {
$scope.objectURL = url;
} else if (url) {
$http.get(url, {
responseType: 'arraybuffer',
cache: true,
headers: {
'accept': 'image/webp,image/*,*/*;q=0.8'
}
})
.then(function (response) {
var blob = new Blob(
[response.data], { type: response.headers('Content-Type') }
);
$scope.objectURL = URL.createObjectURL(blob);
});
}
});
}
};
}]);
})();