gpt4 book ai didi

angularjs - 从 Angular cacheFactory 获取 key

转载 作者:行者123 更新时间:2023-12-03 14:45:29 24 4
gpt4 key购买 nike

当我列出 $cacheFactory 对象时,它有几种方法,但我没有看到实际的键/值缓存。

假设您正在查看 $http 缓存, $cacheFactory($http) 如何获取键列表或理想情况下当前缓存的键和值?

最佳答案

使用 $cacheFactory 的装饰器添加 getKeys方法:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<body ng-app="decorateExample">

<script>
function cacheProvider($provide)
{
// monkey-patches $templateCache to have a keys() method
$provide.decorator('$templateCache', ['$delegate', cacheDelegator]);
}

function cacheDelegator($delegate)
{
var keys = [], origPut = $delegate.put;

$delegate.put = function(key, value)
{
origPut(key, value);
keys.push(key);
};

// we would need cache.peek() to get all keys from $templateCache, but this features was never
// integrated into Angular: https://github.com/angular/angular.js/pull/3760
// please note: this is not feature complete, removing templates is NOT considered
$delegate.getKeys = function()
{
return keys;
};

return $delegate;
}

angular.module('decorateExample', []);

angular.module('decorateExample').config(['$provide', cacheProvider]);
</script>

</body>


引用文献
  • AngularJS Issue #3760: Update cacheFactory.js: Added peek() for returning all keys
  • Decorate Core Directives in Angular
  • 关于angularjs - 从 Angular cacheFactory 获取 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28949838/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com