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

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


当前回答

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

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

其他回答

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

$route.reload();

根据AngularJS文档:

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

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

美元注入状态

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以及你可以称之为刷新或重新渲染。

试试下面的方法:

$route.reload(); // don't forget to inject $route in your controller
$window.location.reload();
location.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();
}

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

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