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.

5
  • possible duplicate of AngularJS: Multiple views with routing without losing scope Commented May 21, 2014 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. Commented May 21, 2014 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. Commented May 21, 2014 at 14:27
  • Persistent data is already stored in a service. But how to handle the reinitialization of the child controller? Commented May 21, 2014 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. Commented May 21, 2014 at 14:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.