[英]Set $httpProvider.defaults.cache in config block
How can I set $httpProvider.defaults.cache
in config block in my application? 如何在应用程序的配置块中设置
$httpProvider.defaults.cache
? Whenever I tried I was receiving "ReferenceError: $cacheFactory is not defined" error. 每当我尝试时,都会收到“ ReferenceError:未定义$ cacheFactory”错误。
So now, I am trying to find out alternative methods to get this done. 所以现在,我正在尝试找出替代方法来完成此任务。
Hence please advice what are the workarounds to achieve this. 因此,请提出实现此目标的解决方法。
If you read this section on $http caching , it says: 如果您在$ http caching上阅读了本节,则会显示:
You can change the default cache to a new object (built with $cacheFactory) by updating the
$http.defaults.cache
property.您可以通过更新
$http.defaults.cache
属性,将默认缓存更改为新对象(使用$ cacheFactory生成)。 All requests who set their cache property to true will now use this cache object.现在,将其缓存属性设置为true的所有请求都将使用此缓存对象。
Notice how you can do it with $http
instead of $httpProvider
. 请注意如何使用
$http
而不是$httpProvider
。 The trick is to not set the default cache in config, but rather in a run block, or some other service/factory you have created. 诀窍是不要在配置中设置默认缓存,而要在运行块或您创建的其他服务/工厂中设置默认缓存。 For example:
例如:
app.run(function($http, $cacheFactory){
$http.defaults.cache = $cacheFactory('myCache', { capacity: 50 });
});
After reading the docs again, I realised I misread them originally. 再次阅读文档后,我意识到我原来是看错了。 If you read the docs carefully for the default, it reads:
如果您仔细阅读默认文档,则其内容为:
defaults.cache - {Object} - an object built with $cacheFactory
defaults.cache-{Object}-使用$ cacheFactory构建的对象
This is poorly written, but I think that it means that you pass in the object that you would use to create a cache using $cacheFactory
. 这写得不好,但是我认为这意味着您传入了使用
$cacheFactory
创建缓存的对象。 For example: 例如:
$httpProvider.defaults.cache = { /* your cache config here */ };
I believe the provider will utilise $cacheFactory
behind the scenes rather than you calling it explicitly. 我相信提供程序将在后台利用
$cacheFactory
,而不是您显式调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.