如何在angularjs中删除缓存

How to remove the cache in angularjs?

本文关键字:删除 缓存 angularjs      更新时间:2023-09-26

我需要你总是清除缓存。但是当我创建、更新或删除客户端时,总是得到相同的结果。只有当我手动删除缓存时(Ctrl+Shift+Supr),我才能看到新的数据。

.factory('Clients', ['$resource', function ($resource) {
    return $resource(pathApi, {}, {
        query: {
            method: 'GET',
            isArray: false
        }
    });
}])

angular.module('app.controllers').controller('controller', ['$scope','Clients', function ($scope,  Clients) {
            Clients.get().$promise.then(
                //success
                function (value) {
                    $rootScope.clients= value;
                },
                //error. 
                function (error) {
                   alert(error);
                }
            );
  }]);

这似乎对我有用:

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

解决方案如下:为请求添加一个时间变量:params: {'foobar': new Date().getTime()}

.factory('Clients', ['$resource', function ($resource) {
    return $resource(pathApi, {}, {
        query: {
            method: 'GET',
            isArray: false,
            params: { 'foobar': new Date().getTime() }
        }
    });
}])