0
プロミスを使用し、サーバーからの応答をキャッシュしました。しかし、私はキャッシュをクリアして、サーバーに再度リクエストを出すときにキャッシュされた応答を与え続けます。私はタスクがリフレッシュタスクでnullに設定されていることを知っていて、サーバーは再び呼び出されていますが、応答は変化していません。ページをリロードしてから新しい応答を得るまではありません。プロミスを使用してサーバーから更新されたデータを取得する
$scope.refreshTask = function(){
// update controller to inject $httpDefaultCache
$httpDefaultCache.removeAll();
listService.refreshTask();
$scope.newTask();
};
あなたはまた、削除する$httpDefaultCache.remove('http://someurl.com/a/b/c')
を呼び出すことができます。あなたがそうのように、$httpDefaultCache
からその項目を削除する必要があり
工場list.js
app.factory('listService', function($http, $q, $cookies) {
//Create a class
function listService() {
var self = this;
self.task = null;
self.getTask = function() {
var deferred = $q.defer();
//If we already have the data, we can resolve the promise.
if(self.task !== null) {
console.log("cache");
deferred.resolve(self.task);
} else {
//Get the data from the server.
$http.get('http://dstm.herokuapp.com/api/tasks')
.success(function(data) {
self.task = data;
console.log(self.task);
console.log("server");
deferred.resolve(data);
})
.error(function(response) {
console.log(response);
deferred.reject(response);
});
}
//return the promise.
return deferred.promise;
};
self.refreshTask = function(){
self.task = null;
console.log(self.task);
}
}
return new listService();
});
app.controller('feedController', function($route, $scope, authFact, $location, $cookies, dataService, fileReader,listService) {
$scope.task = "";
$scope.newTask = function(){
listService.getTask()
.then(
/* success function */
function(data) {
$scope.task = data;
},
/* error function */
function(result) {
console.log("Failed to get the name, result is " + result);
});
// $cookies.put('task', response.data);
// $scope.task = $cookies.get("task");
};
$scope.newTask();
$scope.refreshTask = function(){
listService.refreshTask();
$scope.newTask();
};
HTML
<button ng-click="refreshTask()" id="newTask" type="button">New Task</button>
ブラウザの[ネットワーク]タブには何を教えてくれありませんか?実際のリクエストが200件で、レスポンスデータがあなたの期待に合っていると言いますか? – bhantol