byCoder
byCoder

Reputation: 9184

AngularJS controller is caching in Firefox and IE: how to disable?

In my angular application I have the following controller (I have deleted some methods due privacy policy):

.controller('ArticleCtrl', ['$http', '$scope', '$location', '$localStorage', '$q', '$templateCache', 'authService',
    'uploaderService', 'settings',
    function($http, $scope, $location, $localStorage, $q, $templateCache, authService, uploaderService, settings) {

      $scope.isAuth = authService.checkAuthStatus() || false;

      if ($scope.isAuth == false) {
        $location.path('/signin');
      }


      $scope.username = $localStorage.authStatus.userName;


      $scope.getCompany = function(id) {
        $http.get(settings.apiBaseUri + '/app/' + id, {
            headers: {
              'Content-Type': 'application/json',
              'Cache-Control': 'no-cache'
            }
          })
          .success(function(response) {
            $scope.company = response;
            $scope.company.Email = $scope.username;
          })
          .error(function(data, status, headers, config) {
            console.log('operation failed, status: ' + data);
            $location.path('/signin');
          });
          $scope.$apply();
      };


      if ($scope.isAuth == true) {
        $scope.company = $localStorage.selectedCompany;
        $templateCache.removeAll();
        $scope.getCompany($localStorage.selectedCompany.Id);
      }
    }
  ]);

I have spend a lot of time, but still I don't understand why does only this contoller gets cached (other controllers were made via copy-paste).

But when this method is called for the first time: all is ok, in debugger I see that it goes to server via GET method, but when I refresh the page, and then go again to this controller - in Firefox and IE I see that there are no new requests to the server. But why? Only when I refresh the page with Ctrl + F5 all is ok. But users will do not do that, I need working application...

Maybe somebody knows how to fix this issue? How to disable angularjs view and controller caching?

UPD: I see that after update my localstorage isn't changing in IE and Firefox. Why?

Upvotes: 1

Views: 2346

Answers (2)

Deurco
Deurco

Reputation: 530

Sorry, I do not directly answer the question, but what is coming seems important.

I, and other people, strongly discourage to use ngStorage right now.

Indeed, ngStorage, seems very, very handy. You just directly change the object, and voilà, everything works. I used this a bit, this was cool :)

But, sadly, when you try to make an advanced use, or when you take a look at the source code, you see there are several problems. This awesome "immediate localStorage object modification" is made watching stuff with the $rootScope. That's not a good idea for performance. Moreover, you probably saw that some GitHub issues are stating similar sync problems, like you do. Also, be aware that the project is now completely unmaintained. Use such a library in production is a bad idea.

So, you may give a try to another solution to make the link with the localStorage, such as Angular Locker, becoming more and more used. This will lead to some code refactoring, but you future self will thank you to not have used a problematic library.

Upvotes: 2

Onur Gazioğlu
Onur Gazioğlu

Reputation: 511

First be sure that id parameter is correct. And second be sure that the request headers has no-cache. Probably you have request interceptor and this interceptor override the headers. Track this request in firebug for firefox.

If you see the 'no-cache' values check the server out. Maybe server could cache this.

Upvotes: 0

Related Questions