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

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


当前回答

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

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

其他回答

为了记录,强制angular重新渲染当前页面,你可以使用:

$route.reload();

根据AngularJS文档:

导致$route服务重新加载当前路由,即使$location没有改变。 因此,ngView会创建新的作用域,重新实例化控制器。

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

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

在Angular 2之前(AngularJs)

通过路由

$route.reload();

如果你想重新加载整个应用程序

$window.location.reload();

角2 +

import { Location } from '@angular/common';

constructor(private location: Location) {}

ngOnInit() {  }

load() {
    this.location.reload()
}

Or

constructor(private location: Location) {}

ngOnInit() {  }

Methods (){
    this.ngOnInit();
}

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

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

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

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

eg:

而不是下面这些

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

use,

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