Destroying AngularJS $Http.Get Cache

2019-08-16 08:57发布

I can't figure out how to destroy my cache to get a new list from my server.

When I get the first list, it's work perfect, but after inserting informations to my database and sending another get to my server, the browser only show the cached version of my list, without the new data.

I tried to use cacheFactory like this:

$cacheFactory.get('$http').removeAll();

but it doesn't worked.

Here is my angular Module, Service and Controller.

Module myApp

var app = angular.module('myApp', ['ngRoute', 'LocalStorageModule', 'angular-loading-bar', 'smart-table']);

app.config(function ($routeProvider) {

    $routeProvider.when("/home", {
        controller: "homeController",
        templateUrl: "/web/views/home.html"
    });


    $routeProvider.when("/cidades", {
        controller: "cidadesController",
        templateUrl: "/web/views/basico/cidades/cidades.html"
    });


    $routeProvider.otherwise({ redirectTo: "/home" });
});

app.config(function ($httpProvider) {
    $httpProvider.interceptors.push('authInterceptorService');
});
app.run(['authService', function (authService) {
    authService.fillAuthData();
}]);

cidadesService

'use strict';
app.factory('cidadesService', ['$http', '$cacheFactory', function ($http, $cacheFactory) {

    var serviceBase = 'http://localhost:22207/';
    var serviceFactory = {};

    var _getCidades = function () {

        $cacheFactory.get('$http').removeAll(); //This doesn't worked
        return $http.get(serviceBase + 'api/cidades/getall').then(function (results) {
            return results;
        });
    };

    serviceFactory.getCidades = _getCidades;

    return serviceFactory;

}]);

cidadesController

'use strict';
app.controller('cidadesController', ['$scope', 'cidadesService', function ($scope, service) {

    $scope.cidade = {
        id: "",
        nome:"",
    };

    $scope.message = "";

    $scope.getCidades = function () {
        service.getCidades().then(function (results) {

            $scope.cidades = [];
            $scope.collection = [];

            $scope.cidades = results.data;
            $scope.collection = [].concat($scope.cidades);

        }, function (err) {
            $scope.message = err.error_description;
        });
    };

    //Initializing the list
    $scope.getCidades();

}]);

1条回答
2楼-- · 2019-08-16 09:21

I really don't see anything wrong, but in any case you can add unique param for your request to prevent caching like

$http.get(serviceBase + 'api/cidades/getall?unique=' + new Date().getTime())
查看更多
登录 后发表回答