如何使用 AngularJS 重新加载或重新渲染整个页面 [英] How to reload or re-render the entire page using AngularJS

查看:257
本文介绍了如何使用 AngularJS 重新加载或重新渲染整个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After rendering the entire page based on several user contexts and having made several $http requests, I want the user to be able to switch contexts and re-render everything again (resending all $http requests, etc). If I just redirect the user somewhere else, things work properly:

$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(username) 上的一些 $http 请求正在等待响应的那些被取消了,所以我不能使用它.此外,hack $location.path($location.path()) 也不起作用(什么也没发生).

If I use $window.location.reload(), then some of the $http requests on auth.impersonate(username) that are waiting for a response get cancelled, so I can't use that. Also, the hack $location.path($location.path()) doesn't work either (nothing happens).

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

Is there another way to re-render the page without manually issuing all requests again?

推荐答案

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

For the record, to force angular to re-render the current page, you can use:

$route.reload();

根据 AngularJS 文档:

According to AngularJS documentation:

即使 $location 没有改变,也会导致 $route 服务重新加载当前路由.

Causes $route service to reload the current route even if $location hasn't changed.

因此,ngView 创建了新的作用域,重新实例化了控制器.

As a result of that, ngView creates new scope, reinstantiates the controller.

这篇关于如何使用 AngularJS 重新加载或重新渲染整个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆