Angular reload current route and reload the current template

后端 未结 2 1998
北海茫月
北海茫月 2020-12-03 03:35

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.

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 04:09

    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

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

提交回复
热议问题