2016-12-23 3 views
3

내 UI에 AngularJS (1)가 사용됩니다. 기본 페이지의 변경 동작이 필요합니다. 권한이없는 사용자는 로그인 페이지의 다른 경우 -> 특수 페이지로 이동해야합니다.각도 기본 페이지 라우팅

나는 사용자가 권한을 부여받은 경우 특별한 페이지에 액세스 할 수없는 하나의 시간 (단지 예를 들어 로그인에 대한)이 동작하지만 을 설정할 수 있습니다

anonimModule.config(function ($routeProvider, anonimTemplateManager, anonimUrlManager) { 
    $routeProvider 
     .when('/', { 
      templateUrl: anonimTemplateManager.getLoginPath(), 
      controller: 'loginCtrl' 
     }) 

익명의 모듈을 구성. $ rootScope에서 액세스하려했는데, 내 문제를 해결하기 위해 일 때 실패했습니다.

라우팅에 대한 동적 동작을 구현할 수 있습니까? 구현할 예나 아이디어가 있으십니까?

답변

2

당신은 상태 라우팅을 사용할 수 있습니다 그리고 당신은 statechangestart 예를 들어 이벤트 사용할 수 있습니다 : - 사용자가 authrize을 있는지 확인합니다 app.js에서 다음

.state('user.dashboard', { 
     templateUrl: 'pages/my/dashboard/deshboard.html', 
     controller: 'myDashboardController' 
     url: '/dashboard', 
     access: { 
      loginRequired: true 
     } 

하고 $를 사용할 수 stateChangeStart 일부 인증 서비스 당신이 응용 프로그램에 로그인 할 때 로컬 저장소의 권한 부여 데이터를 저장 상태 여부를

$rootScope.$on('$stateChangeStart', 
      function (event, toState) { 
       var authorised; 
       if (toState.access !== undefined) { 
        authorised = authorizationServie.authorize(toState.name); 
        if (authorised === false) { 
         $state.go("loginState"); 
         event.preventDefault(); 
        } 
       } 
      }); 
3

그렇지 않으면 사용하십시오.

그냥과 같이, 그것을 체인 :

.when('/foo', { 
    templateUrl: 'foo.html', 
    controller: fooController 
) 
.when('/bar', { 
    templateUrl: 'bar.html', 
    controller: barController 
) 
.otherwise(
    redirect: '/foo' 
) 
+0

Bas는 사용자에게 권한이 부여되었는지 여부를 어떻게 확인합니까? – Sergii

+0

서비스 작성을 시도 할 수 있습니다. 사용자가 권한이 부여 된 경우 권한 부여를 확인하여 부울 (또는 사용자가 권한이 있음을 알 수있는 다른 var)을 설정하고 컨트롤러에서 확인합니다 –

2

당신은 또한 당신의 anonimTemplateManager.getLoginPath() 방법으로 다른 페이지로 이동 $state.go('/url')를 작성할 수 있습니다. 같은 작업

몇 가지 다른 방법 $state.go('/url')

    • $location.url();
    • $window.location.href = '/index.html';

    모든 노력을하지.

  • 1

    사용자가 로그인 페이지 또는 디스플레이에, 응용 프로그램에서 응용 프로그램의 필수 경로를 로그인하지 않는 경우, 언제든지 상태 변화를 확인해야 액세스 것을 알림 ...이 작업을 수행하는:

    var app=angular.module("myApp",["LocalStorageModule"]); 
    app.run(function($rootScope,$state,localStorageService){ 
        rootScope.$on("$stateChangeSuccess", function (event, toState, parameters, from) { 
        if(localStorageService.get("authorizationData") !== 'logined') 
         $state.go("loginPage"); 
        }); 
    } 
    

    심지어는 변수에서 stateChangeSuccess에서 이벤트 parametr와 이전 상태를 저장하고 현재 상태로 돌아 오는에 대한 rooScope이 저장할 수 있습니다.