在angularjs缓存图像 - ngRepeat [英] caching images in angularjs - ngRepeat

查看:526
本文介绍了在angularjs缓存图像 - ngRepeat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何实现AngularJS一个图像缓存,还是有办法一次加载图像,并用它多次在同一应用程序

I wanted to know how to implement a image cache in AngularJS, or a way to load the image once and use it many times in the same application

我要显示 UI选目录列表。下拉可能包含的文件夹或文件。对于文件夹,我想显示一个文件夹图标和文件,我想显示的文件图标。我使用的NG-重复迭代文件和文件夹的阵列

I have to show a directory listing in ui-select . The drop down may contain folders or files. For folders I want to show a folder icon and for files I want to show a file icon. I am using ng-repeat to iterate the array of files and folders

[ {name: "abc/", folder: true}, {name: "pqr.txt", folder: false}, ......]

在NG-重复,我有一个NG-如果它显示了一个文件夹图标,如果文件夹的属性是别人真实的它显示一个文件图标。现在,我使用的标签加载图像。该图像是在我的本地目录中,它会减慢性能?我应该使用一个缓存?

Inside the ng-repeat, I have a ng-if which shows a folder icon, if folder-attribute is true else it shows a file-icon. Right now, I am using tag to load the image. The image is in my local directory, will it slow down the performance? Should I use a cache?

推荐答案

如果你没有通过 $ HTTP 服务中使用您可能使用的 $ cacheFactory 服务。在内部,
在$ cacheFactory创建一个默认缓存对象,即使我们没有明确创建一个。

If you are not using through an $http service you might use the $cacheFactory service that generates cache objects for all Angular services. Internally, the $cacheFactory creates a default cache object, even if we don’t create one explicitly.

然后就可以使用了方法,可以把任何JavaScript对象值的键(字符串)到
缓存。

Then you can use the the put method which allows to to put a key (string) of any JavaScript object value into the cache.

cache.put(key, value);

您可以通过

cache.get(key);

或者,如果您使用的是通过HTTP $服务,您可以通过设置缓存参数为真启用缓存:

$http({
   method: 'GET',
   url: '/api/users.json',
   cache: true
});

这篇关于在angularjs缓存图像 - ngRepeat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆