1

Using AngularJS and ui-router. I need to load a different header based on the current state (the user is logged or not).

<div ui-view="header"></div>
<div ui-view="container"></div>

Here are some of the states I'm having:

  • root - everything is based on it
  • root.protected - have some special resolve statements to access protected pages when the user is logged
  • root.protected.home - a protected page
  • root.login - an unprotected login page

How can I define different header (view + controller) based on the state?

There should be one header the user tries to access root.login, and different one accessing root.protected.home.

1 Answer 1

2

You can define two templates: one for "protected" and one for "login":

    $stateProvider
        .state("root", {
            abstract: true,
            template: "<ui-view/>"
        })
        .state("root.protected", {
            abstract: true,
            template: '<div ui-view="header"></div><div ui-view="container"></div>'
        })
        .state("root.protected.home", {
            url: "/home",
            views: {
                header: { templateUrl: "/home-header.html" },
                container: { templateUrl: "/home-container.html" }
            }
        })
        .state("root.login", {
            url: "/login",
            templateUrl: "/login.html",
            controller: "loginController"
        });

You can write whole login html code in login.html. The better solution is to have a state named "unprotected" and define two ui-view for it:

        .state("root.unprotected", {
            abstract: true,
            template: '<div ui-view="header"></div><div ui-view="container"></div>'
        })
        .state("root.unprotected.login", {
            url: "/login",
            views: {
                header: { templateUrl: "/login-header.html", controller: "loginHeaderController" },
                container: { templateUrl: "/login-container.html", controller: "loginContainerController" }
            }
        });

And also a more better solution is to not define a ui-view for header part of unprotected and have only one ui-view for its container. With this approach you don't need to define header for every unprotected view (if you have any). So you just need to write container template.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your response. While I wanted to use the same base template, for both root and root.protected it helped me to solve the issue realizing I have to replace the header view at root.protected state. $stateProvider.state("root.protected", { abstract: true, views: { 'header@': { templateUrl: emisConfig.baseDir + "/views/partials/header-protected.html", } },

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.