Stop AngularJS from caching html between routes

I have two routes on a page connected with Angular JS.

One page has a form from which you can save some information, Angular doesn't seem to ask for html when switching between routes.

I've tried doing $ httpProvider.defaults.cache = false;

Basically for one route I don't want Angular to do html caching, for other routes it is really good.

The 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 removed the incorrect ui manipulation code here.

+3


source to share


3 answers


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

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

      

Hope it solves your problem as well.



you can try to add query string with url

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

      

+11


source


I know its late, but it might help someone

I just used this in front of my routes and it worked.



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

      

+1


source


After searching many sites, I did not find the correct solution to this question, but observing the question and answer in this post, I made the following solution than solved my problem (not at all).

If I double click on a product link link, the second click does not reload / evaluate the content, but if I click on another link and then on the produtcs link, angular reload the content of that.

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

            }]);

      

0


source







All Articles