0

I have a controller that deals with navigation bar (user staff etc.) that wraps all other controllers in my code.

<div class="container" ng-controller="wrapCtrl">

... navbar...

<div ng-view></div>

</div>

Basically I want to keep the navbar on top throughout the script and it will contain view links and handle some user related functions. This has caused me a lot of trouble since everything else (login, dashboard) etc. have their own controllers. Updating the navbar in sync with other controllers is quite problematic. Now I am struggling with my logout function which is handled in wrapCtrl. Logout function simply removes tokens and redirects the user to login page with $location.path('/login'). The the thing is, when location change fires in parent controller (being wrapCtrl), child controller (which is dashboard controller) reinitializes before the actual redirection. Since the user is logged out during location change, this reinitialization causes all the functions in dashboard controller to go mad. I don't think it is necessary but I get flamed if I don't do it so here is the logout function

$scope.logout = function logout() {
            if (AuthenticationService.isLogged) {
                localStorageService.remove('token');
                UserInfoService.setUsrName("");
                $location.path("/login");
            }
        }

Is this supposed to be so problematic as it is now? Or is this (nested controllers?) not the right way to handle my situation? I am aware of the ui-router. But I want to know if this is solvable without using yet another module.

krypt
  • 439
  • 1
  • 4
  • 13
  • possible duplicate of [AngularJS: Multiple views with routing without losing scope](http://stackoverflow.com/questions/16085446/angularjs-multiple-views-with-routing-without-losing-scope) – Sergiu Paraschiv May 21 '14 at 14:19
  • I am aware of the ui-router. But I want to know if this is solvable without using yet another module. Thanks. – krypt May 21 '14 at 14:24
  • No. Both the default router and ui-router destroy the controller instance on route respectively state change. The difference is that ui-router does not destroy controller instances that don't change in the transition from one state to another. Either store persistent data in services or use ui-router or some other alternative. – Sergiu Paraschiv May 21 '14 at 14:27
  • Persistent data is already stored in a service. But how to handle the reinitialization of the child controller? – krypt May 21 '14 at 14:31
  • The child controller is constructed any time it is needed (route/state change). Keep the persistent data in a service and reinitialize it when constructing the controller. There's no magic here if you already know how to use services to persist data. – Sergiu Paraschiv May 21 '14 at 14:33

0 Answers0