ui-router动态路由问题

时间:2016-11-07 11:50:18

标签: javascript angularjs angular-ui-router angular-ui state

是否使用angularJS 1.5和ui.router动态定义状态和路由? 我的意思是从后端服务器获取数据,然后填充ui-router参数,例如state,URL ...... 我试图将它们放在运行部分中,但由于从服务器检索的数据在需要时无法使用,因此它无法正常工作。这就是我正在做的事情

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
            }
        });
})

任何帮助都是真正的帮助

1 个答案:

答案 0 :(得分:0)

此处描述了要走的路

AngularJS - UI-router - How to configure dynamic views

代码段:

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();
});

检查更多并正常工作的there

相关问题