2016-02-02 265 views
1

在我的應用程序必須基於登錄角色 我的登錄控制器重定向頁面

app.controller('LoginController',function(loginService, $rootScope,$scope, $http,$location) { 

    $scope.login = function() { 
     $scope.log=loginService.getLogin($scope.emailId , $scope.password). 
     then(function (response) { 
      console.log($scope.log); 
      console.log(response) 
        if (response.data.LoginVerificationResult.length === 0) { 
         alert('details are not Available for this emailId'); 
         $scope.error=true; 
        } else { 
         $rootScope.name=response.data.LoginVerificationResult[0].UserName; 


         sessionStorage.setItem("User Id",response.data.LoginVerificationResult[0].UserID); 
         sessionStorage.setItem("UserName",response.data.LoginVerificationResult[0].UserName); 
         sessionStorage.setItem("UserType",response.data.LoginVerificationResult[0].UserType); 

          $scope.UserType = sessionStorage.getItem("UserType"); 
          console.log($scope.UserType +"from login controller") 
         //$location.path('/dashboard') 
          if ($scope.UserType =='Doctor') { 
           $location.path('/empRegister') 
          } 
          else ($scope.UserType =='Patient') { 
           $location.path('/patientRegister') 
          } 

        } 

     }); 
    }; 
}); 

我RouteProvider

app.config([ '$routeProvider', function($routeProvider) { 

    $routeProvider.when('/', { 
     templateUrl : 'app/components/login/login.html', 
     controller : 'LoginController' 

    }).when('/patientRegister', { 
     templateUrl : 'app/components/patientRegister/patientRegistration.html', 
     controller : 'patientRegisterCtrl' 

    }).when('/empRegister', { 
     templateUrl : 'app/components/hrRegister/empRegistration.html', 
     controller : 'empRegisterController' 

    }).when('/updateProfile', { 
     templateUrl : 'app/components/profileUpdate/update.html', 
     controller : 'profileUpdate' 

    }).when('/editprofile1', { 
     templateUrl : 'app/components/profileUpdate/editprofile1.html', 
     controller : 'profileUpdate' 

    }).otherwise({ 
     redirectTo : "/" 
    }); 
} ]) 

從它工作正常,是上面的代碼重定向頁面這是我正在做的正確的方式。如果有任何替代請建議我

+0

它似乎好 –

+0

似乎好,但爲什麼要使用不同的形式來更新我的意思是update.html和editprofile1.html 我覺得應該有一種形式。 – MasoodUrRehman

+0

@MasoodRehman都是不同的功能 – Sudhir

回答

0

你可以提到所有狀態所需的角色,同時定義狀態

$routeProvider. 
     .when('/updateProfile', { 
      templateUrl : 'app/components/profileUpdate/update.html', 
      controller : 'profileUpdate', 
      data:{ role:"admin"} 
     }).when('/editprofile1', { 
      templateUrl : 'app/components/profileUpdate/editprofile1.html', 
      controller : 'profileUpdate', 
       data:{ role:"user"} 
     }) 

然後你可以聽路由改變事件。

$rootScope.$on('$routeChangeStart', function(e,next, current) { 
     var user ={} // user object from any model 
     if(user.role!=next.data.role){ 
      // do stuff you want 
     } 
}); 
+0

如何獲得這個值data:{role:「admin」}和data:{role:「user」} – Sudhir

+0

這些值將是靜態的,你必須決定哪個狀態可以被哪個角色訪問。假設您希望管理員訪問個人資料頁面並提及角色:「user」。 – rahulgarg

+0

一個主要問題是,如果我登錄作爲醫生的URL將http:// localhost:8085/HMS /#/ empRegister醫生頁面將打開,如果我更改empRegister到患者寄存器在url然後患者頁面將打開如何限制此,我是非常新的角,所以只問... – Sudhir