Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 444
Sumit Maingi Avatar asked Jun 28 '15 05:06

Sumit Maingi


2 Answers

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();
like image 148
Parmod Avatar answered Oct 21 '22 11:10

Parmod


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();
   });
});
like image 20
Alaa mohammed Avatar answered Oct 21 '22 12:10

Alaa mohammed