23

I'm trying to disable the cache in my AngularJS app, but it isn't working with the following code:

$http.get("myurl",{cache:false})

When I use "myurl&random="+Math.random(), the cache is disabled; but, I'd like a different approach.

2

4 Answers 4

42

This is already answered here.

Pasting code snippet from the link for your reference.

myModule.config(['$httpProvider', function($httpProvider) {
    //initialize get if not there
    if (!$httpProvider.defaults.headers.get) {
        $httpProvider.defaults.headers.get = {};    
    }    

    // Answer edited to include suggestions from comments
    // because previous version of code introduced browser-related errors

    //disable IE ajax request caching
    $httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
    // extra
    $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
    $httpProvider.defaults.headers.get['Pragma'] = 'no-cache';
}]);
Sign up to request clarification or add additional context in comments.

4 Comments

I recommend not using Pragma one. it's not a standard (apparently) and may cause problems with Cross Origin requests. Just finished a 2 hour debug session with friends to understand that was the cause of the problem.
@guymograbi What do you recommend then?
@Schoof does the problem still occur if you simply remove Pragma?
on chrome back button I use to get json bcoz of the same URL, from the above code I solved it Thank you!!
13

Here is what I did, simply change it to

 $http.get("myurl",{headers:{'Cache-Control': 'no-cache'}})

Comments

0

try like this

  $state(' 'app.name', {
 url: '/name',
cache: false,
 controller:'MainCtrl',
 templateUrl:'templates/name.html'
 })

1 Comment

not sure how this answer applies. The question is about http get. not templates.
-1
myApp.config(function ($routeProvider) {
        $routeProvider.
            when('/', {controller: 'MyCtrl', templateUrl: '/eshop/myConfig'})
})

.controller('MyCtrl', function ($scope, $templateCache) {
    $templateCache.remove('/eshop/myConfig');
    // or
    $templateCache.removeAll();
});

i didn't test it.i found something in this url.Have a look at this Angularjs - how to clear $routeProvider's caches of templateUrl

Comments

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.