0

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 Answer 1

1

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())
Sign up to request clarification or add additional context in comments.

1 Comment

Man, you deserve a cookie! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.