2017-11-29 12 views
0

ログイン後のページはメインページにリダイレクトされません。 $window.locationを使用すると、条件ログインの動作を確認しました。 angular-routeを使用して、ログインしていないユーザーがメインメニューにアクセスしないようにします。 $window.locationを使用すると、ユーザーはログインせずにメインメニューにアクセスできます。ここでは、ここに私のjsファイル角度経路はリダイレクトされません

app.config(function($routeProvider){ 
     $routeProvider 
     .when('/',{ 
     templateUrl:'index.html' 
     }) 
     .when('/mainmenu',{ 
    resolve:{ 
     "check":function($location,$rootScope){ 
     if (!$rootScope.loggedIn) { 
      $location.path('/'); 
     } 
     } 
    }, 
    templateUrl:'content/mainmenu2.html' 
    })  
.otherwise({ 
     redirectTo:'/' 
     }); 
    }); 

    app.controller("logincont", ['$scope','$http','$window','$rootScope','$location',function($scope,$http,$window,$rootScope,$location){ 
    $scope.login = function() { 
      $http.get('http://localhost:8089/MonitoringAPI/webresources/login?a='+$scope.userid+'&d='+$scope.password).then(function(response){ 
      $scope.reslogin = response.data; 
      if($scope.reslogin == null) 
      { 
       alert('Login Incorrect'); 
      } 
      else 
      { 
       $rootScope.loggedIn = true; 
       $location.path('/mainmenu'); 

      } 
     }); 
      }; 

      }]); 

は、あなたがルート上の旗allowUnauthを使用することによって、あなたがいないもの、それを行うことができます私のhtml

<form> 
     <h1 class="tengah"> Form Login </h1> 
     <div class="form-group"> 
      <label for="inputId">User Id</label> 
      <input type="text" class="form-control" ng-model="userid" placeholder="userid" required> 
     </div> 
     <div class="form-group"> 
      <label for="inputPassword">Password</label> 
      <input type="password" class="form-control" ng-model="pass" placeholder="Password" required>{{password}} 
     </div> 
     <button type="submit" class="btn btn-primary" ng-click="login()"> 
      <span class="fa fa-sign-out"></span>Login</button> 
    </form> 

答えて

1

のようなものを試してみてくださいされていますたとえばloginの認証を行い、次にルーティングを変更します。 -

app.config(function($routeProvider){ 
     $routeProvider 
     .when('/',{ 
     templateUrl:'index.html', 
     allowUnauth:true 
     }) 
     .when('/mainmenu',{ 
    templateUrl:'content/mainmenu2.html' 
    }).otherwise({ 
     redirectTo:'/' 
     }); 
    }); 

var unauthRoutes= []; 
    angular.forEach($route.routes, function(route, path) { 
     // push route onto unauthRoutes if it has a truthy allowUnauth value 
     route.allowUnauth && (unauthRoutes.push(path)); 
    }); 

    $rootScope.$on('$routeChangeStart', function(event, nextLoc, currentLoc) 
    { 
     var atuhRoute= (-1 === unauthRoutes.indexOf($location.path())); 
     if(atuhRoute && !$rootScope.loggedIn) { 
      $location.path('/'); 
     } 
    }); 

Working plunker

+0

をplunkあなたがplunk @jitender –

+1

@AndikaRistianNugraha与えることができ、あなたの問題を作成pluker私はaccodingly – jitender

+0

https://plnkr.co/edit/i4WzeYjPI4q6cByyv0QR?p=previewこれは修正されますを与えることができます私の大騒ぎ@jitinder –

0

"use strict"; 

configureApp.$inject = ['$routeProvider']; 
function configureApp($routeProvider) { 
    $routeProvider 
    .when('/mainmenu', { 
     resolve: { 
     ensureAthenticated: (function() { 
      ensureAthenticated.$inject = ['$rootScope']; 
      function ensureAthenticated($rootScope) { 
      if(!$rootScope.loggedIn) { 
       return Promise.reject('not athenticated'); 
      } 
      else return Promise.resolve(); 
      }; 
      return ensureAthenticated; 
     }()) 
     }, 
     templateUrl:'content/mainmenu2.html' 
    }) 
    .otherwise('/'); 
} 

app.config(configureApp); 
+0

あなたは@Aluanハダッド –

関連する問題