Sumit Maingi
Sumit Maingi

Reputation: 2263

Stop AngularJS from caching html between routes

I have two routes in a page wired up with Angular JS.

One page has a form from which you can save some information, angular seems to be not requesting for the html when I switch back and forth between the routes.

I have tried doing $httpProvider.defaults.cache = false;

Basically for one route I don't want Angular to be caching the html, for the other routes it is actually a good thing.

Code is given here:

angular.module('userAccount', ['ngRoute', 'ngAnimate'])
.config(['$routeProvider', '$locationProvider', '$httpProvider',
  function ($routeProvider, $locationProvider, $httpProvider) {
      //$httpProvider.defaults.cache = false;
      $locationProvider.hashPrefix('');
      $routeProvider
        .when('/UserProfile/:action', {
            reloadOnSearch: false,
            cache: false,
            templateUrl: function (params) {
                return '/UserProfile/' + params.action;
            },
            controller: 'UserProfCtrl'
        })
        .when('/UserDashboard/:action', {
            templateUrl: function (params) {
                return '/UserDashboard/' + params.action;
            },
            controller: 'UserDashCtrl'
        })
        .otherwise('/UserDashboard/Index');
  }])
.controller('MainCtrl', ['$scope', '$route', '$routeParams', '$location',
  function ($scope, $route, $routeParams, $location) {
      $scope.$on('$routeChangeStart', function (next, current) {

      });

      this.$route = $route;
      this.$location = $location;
      this.$routeParams = $routeParams;
  }])
.controller('UserProfCtrl', ['$routeParams', function ($routeParams) {
    this.name = "UserProfCtrl";
    this.params = $routeParams;
}])
.controller('UserDashCtrl', ['$routeParams', function ($routeParams) {
    this.name = "UserDashCtrl";
      this.params = $routeParams;
    }]);

Note that i have removed some unrelated ui manipulation code here.

Upvotes: 7

Views: 20650

Answers (3)

Alaa mohammed
Alaa mohammed

Reputation: 161

I know its late but it may help someone

I just used this before my routes and it worked.

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

Upvotes: 3

jcromeros1987
jcromeros1987

Reputation: 137

After searching in many sites, i haven't found a correct solution of this, but watching the question and the answer in this post, I have made the next solution than solved my problem (no at all).

If I clicked two consecutive times into the products' link, the second clic don't reload/evaluate the content, but if I clicked on other link and then click on the link produtcs, angular reload the content of this.

            var app = angular.module("myApp", ["ngRoute"]);

            app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {

                $routeProvider
                .when("/", {
                    templateUrl : "main.php"
                })
                .when("/productos", {
                    disableCache: true,
                    templateUrl : function (params) {
                        return "productos.php?" + $.now();
                    }
                })
                .when("/contacto", {
                    templateUrl : "contacto.php"
                })

                $locationProvider.html5Mode(true);

            }]);

Upvotes: 0

Parmod
Parmod

Reputation: 1243

I have same problem in my hybrid mobile app and resolved by adding cache: false in my route.js

$stateProvider.state('stateName', {
   cache: false,
   url : '/url',
   templateUrl : 'template.html'
})

Hope it will resolve your problem as well.

you can try to add query string with url

return '/views/' + params.action+'?'+$.now();

Upvotes: 15

Related Questions