각도에서 나는 서버에 전화를 걸었습니다 인증 서비스가 있습니다. 컨트롤러에서 서비스 변수를 설정할 수 없습니다
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;
};
나는이 설명과 함께 왜 작동하는지 이해하지 못합니까?
도움을 주셔서 감사합니다.하지만 코드 부분을 수정 한 경우 var AccessPrivilegesAuth = function() { return this.isAuth; }; 잘 작동했는데 이유를 모르지만 잘 작동합니다. – MohamedAbbas
이 키워드 만 추가하십시오. – MohamedAbbas