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 ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Paulo Galdo Sandoval
  • 2,173
  • 6
  • 27
  • 44

1 Answers1

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);
},
Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335