[英]Angular $http service cache not working properly
我在angular js中有一個$ http服務,它為它啟用了緩存。 首次加載時,應用程序服務會被調用並獲取緩存。 現在,當我從同一頁面的任何地方調用服務時,數據來自緩存,但是當我更改頁面路由並再次從另一個頁面調用服務時,數據來自服務器(我只是更改路由而不刷新頁面)
編輯=>代碼正在工作! 在途中,數據也來自緩存,但由於其他調用很少,因此花費了更多時間。 花費了更多時間,然后我接受了緩存進行響應。如果我從任何click事件中調用它,則將花費2ms到3ms
這是我的服務
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;
});
},
}
}
}
])
來自控制器的呼叫:
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 };
}]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.