在基于几个用户上下文渲染整个页面并发出几个$http请求后,我希望用户能够切换上下文并重新渲染所有内容(重新发送所有$http请求等)。如果我只是将用户重定向到其他地方,事情会正常工作:

$scope.on_impersonate_success = function(response) {
  //$window.location.reload(); // This cancels any current request
  $location.path('/'); // This works as expected, if path != current_path
};

$scope.impersonate = function(username) {
  return auth.impersonate(username)
    .then($scope.on_impersonate_success, $scope.on_auth_failed);
};

如果我使用$window.location.reload(),那么auth.impersonate(用户名)上等待响应的一些$http请求将被取消,因此我不能使用它。此外,黑客$location.path($location.path())也不起作用(什么都没有发生)。

是否有另一种方法可以重新呈现页面,而无需再次手动发出所有请求?


当前回答

我得到了这个工作代码删除缓存和重新加载页面

View

        <a class="btn" ng-click="reload()">
            <i class="icon-reload"></i> 
        </a>

控制器

喷油器:范围、美元,stateParams, templateCache美元

       $scope.reload = function() { // To Reload anypage
            $templateCache.removeAll();     
            $state.transitionTo($state.current, $stateParams, { reload: true, inherit: true, notify: true });
        };

其他回答

我认为最简单的解决方法是,

在每次返回时都要重新加载的路由中添加'/'。

eg:

而不是下面这些

$routeProvider
  .when('/About', {
    templateUrl: 'about.html',
    controller: 'AboutCtrl'
  })

use,

$routeProvider
  .when('/About/', { //notice the '/' at the end 
    templateUrl: 'about.html',
    controller: 'AboutCtrl'
  })

用于重新加载给定路由路径的页面:-

$location.path('/path1/path2');
$route.reload();

我得到了这个工作代码删除缓存和重新加载页面

View

        <a class="btn" ng-click="reload()">
            <i class="icon-reload"></i> 
        </a>

控制器

喷油器:范围、美元,stateParams, templateCache美元

       $scope.reload = function() { // To Reload anypage
            $templateCache.removeAll();     
            $state.transitionTo($state.current, $stateParams, { reload: true, inherit: true, notify: true });
        };

几个月来,我一直在努力解决这个问题,唯一对我有效的解决方案是:

var landingUrl = "http://www.ytopic.es"; //URL complete
$window.location.href = landingUrl;

如果你想在刷新你正在使用的任何服务的同时刷新控制器,你可以使用这个解决方案:

美元注入状态

i.e.

app.controller('myCtrl',['$scope','MyService','$state', function($scope,MyService,$state) {

    //At the point where you want to refresh the controller including MyServices

    $state.reload();

    //OR:

    $state.go($state.current, {}, {reload: true});
}

这将刷新控制器和HTML以及你可以称之为刷新或重新渲染。