2015-02-24 72 views
4

我自己嘗試根據用戶類型登錄後註冊一個未來狀態註冊登錄後,未來狀態。使用UI的路由器額外的將來狀態模塊

例如: 如果user_typebuyer,然後我需要註冊,我從futureStates_buyer.json

保持如果user_typeadmin所有路由器,然後我需要註冊,我從futureStates_admin.json

保持所有路由器實驗1(根據樣品)

示例網址:http://christopherthielen.github.io/ui-router-extras/example/future/

我可以在配置階段設置以下配置。我明白,提供商設置應該在配置階段。

app.config(function($stateProvider, $urlRouterProvider, $futureStateProvider) { 

$futureStateProvider.stateFactory('ngload', ngloadStateFactory); 
$futureStateProvider.addResolve(loadAndRegisterFutureStates); 

}); 

工作演示:http://plnkr.co/edit/9dUm0f6g2V5PM4m7YlTD?p=preview

實驗2(這是我想)

但是,在我的情況,我不想註冊爲整個所有路由器應用程序在配置階段。這將是非常高的配置。所以我在嘗試的是,一旦登錄後,我可以根據用戶類型獲取用戶類型的登錄響應,我需要註冊未來的狀態。

//config phase 
app.config(function($stateProvider, $urlRouterProvider, $futureStateProvider) { 

//Planned to use futureStateProvider later in controller 
    lazyLoad = {stateProvider: $stateProvider, futureStateProvider:$futureStateProvider}; 

}); 


//Login controller 
app.controller('LoginController', ['$scope', '$rootScope', '$state', '$http', function($scope, $rootScope, $state, $http) { 

    $scope.doLogin = function(){ 

      //After successfull login 
      //Service invocation comes here 
      console.log("Logged in!!"); 
      $rootScope.userInfo = {"loggedIn" : true }; 
      $state.go('dashboard'); 

      //Now I want to register future state based on login response i.e user_type 
      // If user type is buyer, I want to registrer futureStates_buyer.jsom 
      // If user type is vendor, I want to registrer futureStates_vendor.jsom 

      lazyLoad.futureStateProvider.stateFactory('ngload', ngloadStateFactory); // register AngularAMD ngload state factory 
      lazyLoad.futureStateProvider.addResolve(loadAndRegisterFutureStates); 

     } 

}]); 

不工作:http://plnkr.co/edit/pCEKLopOqSKghermTHP8?p=preview

任何幫助將非常感激。感謝提前:)

+0

我用你的代碼,但是當我重新加載頁面被重定向我在登錄頁面上。這是問題。你如何解決這個問題? – 2018-02-26 11:13:26

回答

6

我已經改變controller部分代碼如下固定的問題,這是工作的罰款。

//Login controller 
    app.controller('LoginController', ['$scope', '$rootScope', '$state', '$http', function($scope, $rootScope, $state, $http) { 
     $scope.login = {"username":"admin", "password":"admin123"}; 

     $scope.doLogin = function(){ 

      //After successfull login 
      //Service invocation comes here 
      console.log("Logged in!!"); 
      $rootScope.userInfo = {"loggedIn" : true }; 


      //Now I want to register future state based on login response i.e user_type 
      // If user type is buyer, I want to registrer futureStates_buyer.jsom 
      // If user type is vendor, I want to registrer futureStates_vendor.jsom 

      /*   
      var loadAndRegisterFutureStates = function ($http) { 
       // $http.get().then() returns a promise 
       return $http.get('futureStates_buyer.json').then(function (resp) { 
       angular.forEach(resp.data, function (fstate) { 
        // Register each state returned from $http.get() with $futureStateProvider 
        lazyLoad.futureStateProvider.futureState(fstate); 
       }); 
       }); 
      }; 
      */ 

      $http.get('futureStates_buyer.json').then(function (resp) { 
       angular.forEach(resp.data, function (fstate) { 
        // Register each state returned from $http.get() with $futureStateProvider 
        lazyLoad.futureStateProvider.futureState(fstate); 
       }); 

       $state.go('dashboard'); 
     }); 

      lazyLoad.futureStateProvider.stateFactory('ngload', ngloadStateFactory); // register AngularAMD ngload state factory 
      //lazyLoad.futureStateProvider.addResolve(loadAndRegisterFutureStates); 

     } 

    }]); 

工作演示:http://plnkr.co/edit/ZXty2SkmGbuwnpaJoh7C?p=preview

+0

嗨,你可以請分享,當用戶重新加載頁面時你如何維護狀態。 – 2018-02-27 05:52:16