0

is there using angularJS 1.5 and ui.router to define State and routes dynamically? I mean getting the data from a backend sever and then populate the ui-router param such as state, URL ... I tried to use the put them in the run part but it's not working as the data retrieved from the server wasn't available when needed. here is what I'm doing

run(
  function run(Idle, $http, $q, $state, $rootScope) {
    Idle.watch();
    $urlRouterProviderRef.otherwise('/login');

    $urlRouterProviderRef.when("", "/login");
    $http.get(_contextPath + '/routing', {})
    .success(function(data)
    {
        $rootScope.LOGIN_PAGE_CONTROLLER_NAME = data.LOGIN_PAGE_CONTROLLER_NAME;
        $rootScope.LOGIN_PAGE_PAGE_TITLE = data.LOGIN_PAGE_PAGE_TITLE;
        $rootScope.LOGIN_PAGE_STATE = data.LOGIN_PAGE_STATE;
        $rootScope.LOGIN_PAGE_TEMPLATE_URL = data.LOGIN_PAGE_TEMPLATE_URL;
        $rootScope.LOGIN_PAGE_URL = data.LOGIN_PAGE_URL;


    });

    var test = $rootScope.LOGIN_PAGE_STATE;

        $stateProviderRef.state($rootScope.LOGIN_PAGE_STATE, {
            url : $rootScope.LOGIN_PAGE_URL,
            views : {
                "mainbody" : {
                    templateUrl : $rootScope.LOGIN_PAGE_TEMPLATE_URL
                },

            },
            controller : $rootScope.LOGIN_PAGE_CONTROLLER_NAME,
            data : {
                pageTitle : $rootScope.LOGIN_PAGE_PAGE_TITLE,
                authenticate : false
            }
        });
})

any help is really apreciated

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Med
  • 241
  • 1
  • 6
  • 23

1 Answers1

0

The way to go is described here

AngularJS - UI-router - How to configure dynamic views

A code snippet:

var app = angular.module('app', ['ui.router.router']);

app.config(function($urlRouterProvider) {

  // Prevent $urlRouter from automatically intercepting URL changes;
  // this allows you to configure custom behavior in between
  // location changes and route synchronization:
  $urlRouterProvider.deferIntercept();

}).run(function($rootScope, $urlRouter, UserService) {

  $rootScope.$on('$locationChangeSuccess', function(e) {
    // UserService is an example service for managing user state
    if (UserService.isLoggedIn()) return;

    // Prevent $urlRouter's default handler from firing
    e.preventDefault();

    UserService.handleLogin().then(function() {
      // Once the user has logged in, sync the current URL
      // to the router:
      $urlRouter.sync();
    });
  });

  // Configures $urlRouter's listener *after* your custom listener
  $urlRouter.listen();
});

Check more and working plunker there

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335