Angular 重新加载当前路由并重新加载当前模板 [英] Angular reload current route and reload the current template

查看:24
本文介绍了Angular 重新加载当前路由并重新加载当前模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户访问未经授权的私人页面时,比如个人资料,我的后端 302 重定向到控制器操作,该操作提供登录部分而不是个人资料部分.由于它 302 重定向到返回部分的操作,因此 url 地址栏不会从用户尝试访问的页面(/profile")更改.

When a use visits a private page unauthorized, say profile, my backend 302 redirects to a controller action that serves up the login partial in place of the profile partial. Since it 302 redirects to an action that returns a partial, the url address bar doesn't change from the page the user was trying to access ("/profile").

我打算修复"这个问题,但实际上我认为它提供了良好的用户体验,而不是将返回 url 作为查询参数处理.

I was going to "fix" that but actually I think it makes a good user experience instead of dealing with return urls as query params.

这个想法是,一旦他们登录,我只想重新加载当前路由,也就是通过/profile"对配置文件部分执行 GET 请求,然后将其切换回而不是登录部分.

The idea is once they log in I just want to reload the current route aka do a GET request for the profile partial via "/profile" and switch it back in instead of the login partial.

但是,我无法让这个重新加载当前路线"起作用.我尝试了以下所有方法:

However, I can't get this "reload current route" to work. I tried all of the following:

$location.$$compose();
$location.absUrl($location.path());
$location.url($location.path());
$location.path($location.path())
$route.reload();

但是没有用.$route.reload() 似乎是确定的方法,但它也不起作用.遍历路由循环,重新实例化控制器,但不做GET请求重新加载模板

But none work. $route.reload() seems to be the definite way but it also doesn't work. It goes through the route cycle, reinstantiates the controller, but does not do GET request to reload the template

唯一有效的是通过 location.reload() 进行硬刷新,但这并不理想.

The only thing that works is a hard refresh via location.reload() but that is not ideal.

如何强制 angular 重新加载当前路线的模板?

How can I force angular to reload the template for the current route?

推荐答案

好的我在 Github 上找到了 lgalfaso 提供的解决方案(精确粘贴):

Ok I found the solution provided by lgalfaso on Github (exact paste):

模板是缓存,如果用户没有进入页面的权限,那么此检查应在到达控制器之前或之后进行,在控制器内,但不在模板检索上

Templates are cached, if a user does not have the permissions to be in a page, then this check should be done before it reaches the controller or after, within the controller, but not on the template retrieval

如果这是您想要遵循的方式,那么您需要删除调用 reload 之前 $templateCache 中的模板

If this is the way you want to follow, then you need to remove the template from the $templateCache before you call reload

所以这对我有用,因为登录模板实际上被缓存为用户试图访问的模板.因此,删除它并让 angular 重新获取当前路线的正确路线就像一个魅力.

So that worked for me because login template actually gets cached as the template the user was trying to access. So removing it and letting angular re-fetch the correct one for the current route worked like a charm.

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

这篇关于Angular 重新加载当前路由并重新加载当前模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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