销毁AngularJS $ Http.Get缓存 [英] Destroying AngularJS $Http.Get Cache

查看:545
本文介绍了销毁AngularJS $ Http.Get缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何摧毁我的缓存从我的服务器得到一个新的列表。

当我拿到第一个名单,它的工作完美,但插入信息到我的数据库和发送另一个让我的服务器后,浏览器只显示我的列表的缓存版本,没有新的数据。

我试图用cacheFactory这样的:

  $ cacheFactory.get('$ HTTP')的removeAll()。

但它并没有奏效。

下面是我的角模块,服务和控制器。

模块对myApp

  VAR应用= angular.module('对myApp',['ngRoute','LocalStorageModule,角装吧','智能表']);的app.config(函数($ routeProvider){    $ routeProvider.when(/家,{
        控制器:HomeController中
        templateUrl:/web/views/home.html
    });
    $ routeProvider.when(/ Cidades的,{
        控制器:cidadesController
        templateUrl:/web/views/basico/cidades/cidades.html
    });
    $ routeProvider.otherwise({redirectTo:/家});
});的app.config(函数($ httpProvider){
    $ httpProvider.interceptors.push('authInterceptorService');
});
app.run(['authService',函数(authService){
    authService.fillAuthData();
}]);

cidadesService

 使用严格的;
app.factory('cidadesService',['$ HTTP,$ cacheFactory',函数($ HTTP,$ cacheFactory){    VAR serviceBase =的http://本地主机:22207 /';
    变种的ServiceFactory = {};    VAR _getCidades =功能(){        $ cacheFactory.get('$ HTTP')的removeAll()。 //这不工作
        返回$ http.get(serviceBase +'API / Cidades的/ GETALL'),然后(功能(结果){
            返回结果;
        });
    };    serviceFactory.getCidades = _getCidades;    返回服务工厂;}]);

cidadesController

 使用严格的;
app.controller('cidadesController',['$范围,cidadesService',函数($范围,服务){    $ scope.cidade = {
        ID: ,
        诺姆:,
    };    $ scope.message =;    $ scope.getCidades =功能(){
        service.getCidades()。然后(功能(结果){            $ scope.cidades = [];
            $ scope.collection = [];            $ scope.cidades = results.data;
            $ scope.collection = [] .concat($ scope.cidades);        },功能(错误){
            $ scope.message = err.error_description;
        });
    };    //初始化列表
    $ scope.getCidades();}]);


解决方案

我实在看不出什么毛病,但在任何情况下,你可以为你的请求prevent缓存添加独特的参数

  $ http.get(serviceBase +'API / Cidades的/ GETALL?唯一='+新的Date()。的getTime())

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

}]);

解决方案

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

这篇关于销毁AngularJS $ Http.Get缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆