Angular reload current route and reload the current template

后端 未结 2 1950
北海茫月
北海茫月 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条回答
  • 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();
    
    0 讨论(0)
  • 2020-12-03 04:34

    I have noticed that $route.reload() method re-instantiates everything that is setup on your $routeProvider.when("/someUrl",{controller:'SomeController',templateUrl:'SomeView.html'}) template,controller and/or any resolved promises you may have passed within the .when() method.

    Therefore; when you build your app, if you want $route.reload() to reload all the page and re-instantiate controllers you must put everything under your <div ng-view></div> container and include any menus or footers inside the templateUrl file.

    0 讨论(0)
提交回复
热议问题