Вопрос

I use a form to submit values. When I click on the submit button, it calls edit(). Values are stored in the database (with php/mysql). Everything works fine so far.

I retrieve the values on the same partial view. On window.location.reload(); the page will refresh but the values are not instantly updated. After several refresh, it's (When my DevTools is open, the cache is disable... so it's instantly updated)

Without the DevTools open, the cache works even if I put some function to disable the cache.

listingProjectApp.run(function($rootScope, $templateCache) {
  $rootScope.$on('$viewContentLoaded', function() {
     $templateCache.removeAll();
  });
});

listingProjectApp.factory('EditCache', function($cacheFactory) {
 return $cacheFactory('editCache', {
   capacity: 1
 });
});

listingControllers.controller('editProjectCtrl', ['$scope', '$http', '$routeParams',      'EditCache',
function ($scope, $http, $routeParams, EditCache) {
var result = EditCache.get('$http');

if( result ){
    $scope.project = result;
    console.log("Results from cache");
}
else {      
    $http({
        url: "php/getProjectDetails.php",
        method: "GET",
        cache: false,
        params: { 'id': $scope.projectId }
    }).success(function(data) {
        $scope.project = data;
        EditCache.put('$http', data);
        console.log("New results");
    });     
}
console.log(EditCache.info())

$scope.edit = function(project){

    console.log(project);
    $http({
        url: "php/edit.php",
        method: "POST",
        data: project
    }).success(function(id) {
        EditCache.removeAll();
        window.location.reload();
    });

    return false;

}
   }]);
Это было полезно?

Решение

Looks like it's not the AngularJS cache's fault, but rather the browser's: for the browser, your XHR requests are simple HTTP GET requests, and all caching policies are applied to them. To avoid caching the partials, you'll have to either tweak your back-end to send appropriate headers to forbid caching, or use the old random suffix trick: add another dummy parameter to your params (like dummy: 'some-random-string'). This way, every GET request will be unique, and you'll not hit the cache.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top