【问题标题】:Destroying AngularJS $Http.Get Cache销毁 AngularJS $Http.Get 缓存
【发布时间】:2015-12-03 20:42:41
【问题描述】:

我不知道如何销毁缓存以从服务器获取新列表。

当我得到第一个列表时,它工作得很好,但是在将信息插入我的数据库并将另一个 get 发送到我的服务器之后,浏览器只显示我的列表的缓存版本,没有新数据。

我尝试像这样使用 cacheFactory:

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

但它不起作用。

这是我的 Angular 模块、服务和控制器。

模块 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;

}]);

cidades控制器

'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();

}]);

【问题讨论】:

    标签: javascript angularjs browser-cache


    【解决方案1】:

    我真的没有发现任何问题,但无论如何您都可以为您的请求添加唯一参数以防止缓存 喜欢

    $http.get(serviceBase + 'api/cidades/getall?unique=' + new Date().getTime())
    

    【讨论】:

    • 伙计,你应该得到一块饼干!谢谢!
    猜你喜欢
    • 2018-06-18
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2018-07-17
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    相关资源
    最近更新 更多