1

I'm trying to load a navbar according to the user.

To do this, I need to set a dynamic template, but I can't see my $rootScope

$stateProvider
    /*Login*/
    .state('login', {
        url: '/',
        data: {pageTitle: 'Inicio de sesión.'},
        resolve: {},
        views: {
            'navbar': {
                templateUrl: null,
                controller: null
            },
            'body': {
                templateUrl: "views/login.html",
                controller: 'LoginController'
            }
        }
    })
    .state('home', {
        url: '/home',
        data: {pageTitle: 'Home.'},
        views: {
            'navbar': {
                templateUrl: "views/navbar.html", //here dynamic template
                controller: null
            },
            'body': {
                templateUrl: "views/inicio.html",
                controller: null
            }
        }
    })
    .state('perfil', {
        url: '/perfil',
        data: {pageTitle: 'Perfil de usuario.'},
        views: {
            'navbar': {
                templateUrl: function (sessionProvider) {
                    var sessionFactory = sessionProvider.path();
                    return sessionFactory;
                }, // this return a tring, but don't load anything
                controller: null
            },
            'body': {
                templateUrl: "views/usuario/perfil.html",
                controller: 'UsuarioController'
            }
        }
    });

i've tried use a service to load the data, but it didnt work, also tried to load it from the localStorageService, but don't display anything.

is there a single way to load it ?

1
  • Did you tried ng-include? Commented Jun 1, 2016 at 6:31

1 Answer 1

1

There are features for this, like templateUrl and templateProvider. See all details here:

Trying to Dynamically set a templateUrl in controller based on constant

E.g. this could be an example of templateProvider used isntead of the 'templateUrl' static path

templateProvider: function(CONFIG, $templateRequest) {
    console.log('in templateUrl ' + CONFIG.codeCampType);

    var templateName = 'index5templateB.html';

    if (CONFIG.codeCampType === "svcc") {
         templateName = 'index5templateA.html';
    } 

    return $templateRequest(templateName);
},
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, this is exactly what i was looking!
Great to see that, Enjoy mighty UI-Router ;)

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.