阻止AngularJS在路由

时间:2015-06-28 05:43:48

标签: javascript angularjs

我在与Angular JS连接的页面中有两条路线。

一个页面有一个表单,您可以从中保存一些信息,当我在路线之间来回切换时,angular似乎没有请求html。

我尝试过$ httpProvider.defaults.cache = false;

基本上对于一条路线我不希望Angular缓存html,对于其他路线它实际上是一件好事。

代码在这里给出:

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

请注意,我在这里删除了一些不相关的ui操作代码。

2 个答案:

答案 0 :(得分:15)

我在我的混合移动应用程序中遇到同样的问题,并通过在我的route.js

中添加cache:false来解决
$stateProvider.state('stateName', {
   cache: false,
   url : '/url',
   templateUrl : 'template.html'
})

希望它也能解决你的问题。

您可以尝试使用网址

添加查询字符串
return '/views/' + params.action+'?'+$.now();

答案 1 :(得分:0)

在许多网站上搜索后,我还没有找到正确的解决方法,但是在这篇文章中看到问题和答案,我已经制定了下一个解决方案而不是解决了我的问题(根本没有)。 / p>

如果我连续两次点击产品'链接,第二个clic不重新加载/评估内容,但如果我点击其他链接,然后点击链接produtcs,角度重新加载此内容。

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

            }]);