ui-router动态路由问题

ui-router Dynamic routing issue

本文关键字:问题 路由 动态 ui-router      更新时间:2023-09-26

使用angularJS 1.5和ui。路由器动态定义状态和路由?我的意思是从后端服务器获取数据,然后填充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
            }
        });
})

任何帮助都是非常感谢的

方法如下

AngularJS - UI-router -如何配置动态视图

代码片段:

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

查看更多和工作活塞在那里