在基于几个用户上下文渲染整个页面并发出几个$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())也不起作用(什么都没有发生)。

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


当前回答

也许你在声明控制器的依赖项时忘记添加"$route":

app.controller('NameCtrl', ['$scope','$route', function($scope,$route) {   
   // $route.reload(); Then this should work fine.
}]);

其他回答

使用下面的代码,而不向用户发送重载通知。它将呈现页面

var currentPageTemplate = $route.current.templateUrl;
$templateCache.remove(currentPageTemplate);
$window.location.reload();

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

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

eg:

而不是下面这些

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

use,

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

为了重新加载所有内容,使用window.location.reload();与angularjs

查看工作示例

HTML

<div ng-controller="MyCtrl">  
<button  ng-click="reloadPage();">Reset</button>
</div>

angularJS

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.reloadPage = function(){window.location.reload();}
}

http://jsfiddle.net/HB7LU/22324/

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

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

如果你正在使用angular ui-router,这将是最好的解决方案。

$scope.myLoadingFunction = function() {
    $state.reload();
};