最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Stop AngularJS from caching html between routes - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Jun 29, 2015 at 18:11 isherwood 61.1k16 gold badges120 silver badges168 bronze badges asked Jun 28, 2015 at 5:43 Sumit MaingiSumit Maingi 2,2633 gold badges27 silver badges44 bronze badges 2
  • if cache: false didn't work for you then please share your code – Viral Shah Commented Jun 28, 2015 at 7:59
  • I have updated the question with code, the uncommented section is what i had tried. Thanks for your time. – Sumit Maingi Commented Jun 29, 2015 at 8:17
Add a comment  | 

3 Answers 3

Reset to default 15

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

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

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

            }]);
发布评论

评论列表(0)

  1. 暂无评论