gpt4 book ai didi

javascript - Angular $http 服务缓存无法正常工作

转载 作者:行者123 更新时间:2023-11-28 08:20:39 26 4
gpt4 key购买 nike

我在 Angular js 中有一个 $http 服务,它启用了缓存。第一次加载时,应用程序服务会被调用并获取缓存。现在,当我从同一页面的任何位置调用服务时,数据来自缓存,但是当我更改页面路由并再次从另一个页面调用服务时,数据来自服务器(我只是更改路由而不刷新页面)

编辑 =>代码正在运行!在路线上,数据也来自缓存,但由于几乎没有其他调用,因此花费了更多时间。只是花了更多的时间然后我接受缓存来响应。如果我从任何点击事件调用相同的那么它将需要 2 毫秒到 3 毫秒

这是我的服务

commonServicesModule.service('getDetails', ['$http', 'urlc',
function($http, urlc) {

return {
consumer: {
profile: function(id) {
return $http({
url: urlc.details.consumer.profile,
cache: true,
params: {
'consumer_id': id,
'hello': id,
},
method: 'GET',
}).success(function(result) {
return result;
});
},

}
}
}
])

来自 Controller 的调用:

start = new Date().getTime();
/*get user information */
getDetails.consumer.profile('1').then(function(results) {

console.log('time taken for request form listCtrl ' + (new Date().getTime() - start) + 'ms');
});

当我在路由后从其他任何地方调用它时,它需要相同的时间。

最佳答案

尝试将 consumer 对象移动到函数体内,并返回对其的引用,如下所示:

commonServicesModule.service('getDetails', ['$http', 'urlc', function($http, urlc) {

var getConsumer = {
profile: function(id) {
return $http({
url: urlc.details.consumer.profile,
cache: true,
params: {
'consumer_id': id,
'hello': id,
},
method: 'GET',
}).success(function(result) {
return result;
});
}
};

return { consumer: getConsumer };

}]);

关于javascript - Angular $http 服务缓存无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23007958/

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