我想使用角的http緩存,但結果是未定義的。緩存返回一個對象,但用戶緩存未定義。
在main.js控制器
app.controller('exploreController', function($scope, dataService, $cookies, $cacheFactory, $http) {
// dataService.explorePosts();
$scope.explore = function(){
dataService.explorePosts();
var cache = $cacheFactory.get('$http');
console.log(cache);
var usersCache = cache.get('http://dstm.herokuapp.com/api/explore');
console.log(usersCache);
};
$scope.explore();
});
服務在data.js
angular.module('dsnApp')
.service('dataService', function($http, $cookies, $cacheFactory) {
this.explorePosts = function(){
var id = $cookies.get("userId");
$http.get('http://dstm.herokuapp.com/api/explore', {cache: true,
params: {userId: id, page: 1},
})
.then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
console.log(response);
});
};
控制器* *? – MACMAN
您可以創建一個工作** plnkr – Aravind
$ http是asynchronous.nothing將被緩存,直到請求完成,並且您試圖同步訪問緩存 – charlietfl