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

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

问题描述

在使用访问私人网页未经授权的,说的个人资料,我的后端302重定向到担任了登录到位轮廓部分的部分的控制器操作。由于302重定向到一个返回部分的动作,URL地址栏没有从用户试图访问(/配置文件)的页面改变。

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.

如何强制角度重新加载当前路由模板?

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

推荐答案

好吧,我发现通过lgalfaso在Github(精确贴)提供的解决方案:

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

如果这是你要遵循的方式,那么你需要删除
  从$ templateCache模板调用重装前

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

所以,因为登录模板实际上缓存为模板用户试图访问该为我工作。因此,删除它,让角度重新获取正确的当前路线的工作就像一个魅力。

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

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

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