2014-07-09 3 views
0

각도에서 나는 서버에 전화를 걸었습니다 인증 서비스가 있습니다. 컨트롤러에서 서비스 변수를 설정할 수 없습니다

pp.factory('authService', ['api','$cookieStore', function(api, $cookieStore, ACCESS_LEVELS){ 

    var AuthentoctionControllerUrl = "http://localhost:18678/api/Authentication/"; 
    var isAuth; 
    var IsAdmin; 
    function Login(credential){ 
     return api.Post(AuthentoctionControllerUrl + "ByPassword" ,null , credential); 
    }; 

    var Logout = function() { 
     $cookieStore.remove('AuthorizationHeader'); 
     isAuth = false; 
    }; 

    var AccessPrivilegesAdmin = function(user_type){ 
     if(IsAdmin == user_type) return true; 
     return false; 
    }; 

    var AccessPrivilegesAuth = function(){ 
     return isAuth; 
    }; 

    return { 
     Login : Login, 
     Logout : Logout, 
     isAuth : isAuth, 
     IsAdmin : IsAdmin, 
     AccessPrivilegesAuth : AccessPrivilegesAuth, 
     AccessPrivilegesAdmin : AccessPrivilegesAdmin 
    }; 
}]); 

서비스

는 isAuth을하고 난 그 컨트롤러가 값을 설정해야 호출하고 난 디버그 모드에서 볼 때 현재 그들이 undfiend.However입니다 관리자는 두 변수가 있습니다.

app.controller('LoginController', ['$scope', '$location', 'authService','$cookieStore', function($scope, $location, authService, $cookieStore){ 
    $scope.loginData = { 
     EmailAddress : "", 
     password : "" 
    }; 

    $scope.error; $scope.error_exist = false; 
    $scope.login = function(){ 
     authService.Login($scope.loginData).success(function(response){ 
      $cookieStore.put('AuthorizationHeader', response.Token); 
      authService.isAuth = true; 
      authService.IsAdmin = response.IsAdmin; 
      $location.path('categories'); 
     }).error(function(Error){ 
      $scope.error_exist = true; 
      switch(Error.ExceptionMessage){ 
       case "201" : 
        $scope.error = "The emailAddress/password pair don't match an existing member"; break; 
       case "210" : 
        $scope.error = "Value cannot be null missing Email Address and/or password."; break; 
       case "202" : 
        $scope.error = "The email address you are using isn't confirmed. Please see your inbox for further instructions."; break; 
       default : 
        $scope.error = "Error with the server"; 
      } 
     }); 

    }; 
}]); 

문제는 내가 그 메소드를 호출 할 때 그러나 isAuth의 반환 내가 있다고 확신 undfiend입니다 내가 컨트롤러에서 설정 한 isAuth을 반환해야하는 방법 AccessPrivilegesAuth를 호출 완패 변화에 대한 이벤트를 가지고있다 컨트롤러의 값을 authService.isAuth = true; 으로 설정합니다.이 키워드를 공장의 isAuth 및 isAdmin에 추가하여 찾았습니다. 새 코드.

var AccessPrivilegesAdmin = function(user_type){ 
    if(this.IsAdmin == user_type) return true; 
    return false; 
}; 

var AccessPrivilegesAuth = function(){ 
    return this.isAuth; 
}; 

나는이 설명과 함께 왜 작동하는지 이해하지 못합니까?

답변

0

AccessPrivilegesAuth에 액세스 할 때 서버 호출이 완료되지 않아 문제가 해결되지 않은 것 같습니다.

로그인 코드를 사용중인 경로의 해결 기능에 넣어이 문제를 해결할 수 있습니다. 여기

이 블로그에서 몇 가지 예제 코드가 http://odetocode.com/blogs/scott/archive/2014/05/20/using-resolve-in-angularjs-routes.aspx 게시 할 수 있습니다 : 당신이 해결 기능에서 볼 수 있듯이

$routeProvider 
    .when("/news", { 
     templateUrl: "newsView.html", 
     controller: "newsController", 
     resolve: { 
      message: function(messageService){ 
       return messageService.getMessage(); 
     } 
    } 
}) 

를, 서버 및 결과에 대한 호출이 객체 '메시지'가 될 수 있습니다 이 같은 컨트롤러에 직접 주입 :

app.controller('LoginController', ['$scope', 'message', 
    function($scope, message) { 

그리고 메시지가 컨트롤러 내에서이 도움이

희망을 사용할 수 있습니다.

+0

도움을 주셔서 감사합니다.하지만 코드 부분을 수정 한 경우 var AccessPrivilegesAuth = function() { return this.isAuth; }; 잘 작동했는데 이유를 모르지만 잘 작동합니다. – MohamedAbbas

+0

이 키워드 만 추가하십시오. – MohamedAbbas

관련 문제