Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AngularJS2 / Ionic2 : ImageCache Directive to use with imgcache.js
import { Directive, ElementRef, Input } from '@angular/core';
declare var ImgCache: any;
@Directive({
selector: '[image-cache]'
})
export class ImageCacheDirective {
constructor (
private el: ElementRef
) {
// init
}
ngOnInit() {
this.el.nativeElement.crossOrigin = "Anonymous"; // CORS enabling
ImgCache.isCached(this.el.nativeElement.src, (path: string, success: any) => {
console.log('path - '+ path);
console.log('success - '+ success);
if (success) {
// already cached
console.log('already cached so using cached');
ImgCache.useCachedFile(this.el.nativeElement);
} else {
// not there, need to cache the image
console.log('not there, need to cache the image - ' + this.el.nativeElement.src);
ImgCache.cacheFile(this.el.nativeElement.src, () => {
console.log('cached file');
// ImgCache.useCachedFile(el.nativeElement);
});
}
});
}
}
@alshdotme
Copy link

alshdotme commented Feb 28, 2017

@guyaloni

Create a file called declarations.d.ts file and inside this add

declare module 'imgcache.js';

Then in your component add

import ImgCache from 'imgcache.js';

Sorry if you've already done this, I got a hunch you might have not.

@mm580486
Copy link

mm580486 commented Sep 27, 2017

hi thanks for this code
how to use this code on api call ? not problem ?

@smkamranqadri
Copy link

smkamranqadri commented Oct 2, 2017

Cross-origin error :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment