I created an app and it does work great but images load every time I scroll up and down, I looked it in google and found this, the problem is I use angular, there is no such thing as viewModel
in my angular version, how do I apply cache image into template?
Asked
Active
Viewed 1,546 times
1

erfan ghaemian
- 11
- 3
1 Answers
2
You can try this code. It is working for me.
.ts code
import imageCacheModule = require("ui/image-cache");
import imageSource = require("image-source");
var cache = new imageCacheModule.Cache();
private _imageSrc: any;
private imgSource: any;
getImageCache(imageURL) {
cache.placeholder = imageSource.fromResource("res://no-image.png");
cache.maxRequests = 10;
cache.enableDownload()
var images = cache.get(imageURL)
if(images) {
return images
} else {
cache.push({
key: imageURL,
url: imageURL,
completed: (image: any, key: string) => {
if (imageURL === key) {
this.imgSource = imageSource.fromNativeSource(images);
}
}
})
cache.disableDownload();
}
HTMl code
<Image [src]="getImageCache(item.image?.url)" class="item-image"></Image>

Jay Thakkar
- 1,392
- 10
- 19
-
How does this work? `else` part doesn't have any `return` statement. And what is that private `imgSource` property? I'm confused. – Murat Çorlu Aug 10 '18 at 22:20
-
In else 'this.imgSource' will update the image. You can add console and check. – Jay Thakkar Aug 11 '18 at 08:18