2017-05-29 28 views
0

這是我廠的樣子

app.factory('AuthenticationService',['$http', function ($http, $localStorage) { 

    var AuthenticationService = {}; 
    var api = 'http://del1-vm-kohls:8080/Survey' ; 


    AuthenticationService.Login = function(username,password,callback){ 

     $http.post('http://del1-vm-kohls:8080/Survey/user/login',{userId: username, password: password}) 
      .success(function(response){ 
       if(response.token) { 
        // storing token in localstorage if user refreshes 
        $localStorage.currentUser = {userId : username , token: response.token }; 

        // adding token of authentication for futher use in getting data 
        authentication.token = response.token; 

        callback(true); 
       } 
       else{ 
        callback(false); 
       } 
     }); 
    } 

    AuthenticationService.getSurvey = function(token){ 

    } 

    return AuthenticationService; 
}]); 

這是我的控制器看起來像

var app = angular.module('myApp',[]); 

app.controller('myCtrl', ['$scope', '$localStorage', '$http', 'AuthenticationService', function($scope, $localStorage, $http, AuthenticationService){ 

    $scope.username = ""; 
    $scope.password = ""; 
    $scope.login = false; 

    $scope.checkLogin = function(){ 

     AuthenticationService.Login($scope.username, $scope.password, function(result){ 
      if(result === true) 
       { 
        console.login("Logindone"); 
        $scope.login = true; 
        $localStorage.setItem('auth', 'true'); 
        location.href = "../../application/content/index.html"; 

       } 
      else{ 
        $scope.login = false; 
        $localStorage.setItem('auth', 'false'); 
        sweetAlert("", "Invalid Username or Password", "error"); 
      } 
     });   
    }  

}]); 

回答

1

你在工廠的依賴聲明中缺少$localStorage

應該是這樣:

app.factory('AuthenticationService',['$http','$localStorage', function ($http, $localStorage) { 
+0

仍然沒有工作 – CuriousAboutThings

+0

@abhishekgangwar:什麼是不工作的,?如果你有相同的錯誤檢查是否所有的依賴加載到你的'html'正確的順序?或者現在你有其他的錯誤?你的'factory method'中的'authentication.token'是什麼? – anoop

+0

我有和以前一樣的錯誤。我也檢查了依賴關係的順序。並且就authentication.token而言,我試圖將令牌分配給對象AuthenticationService的密鑰。所以基本上它的AuthenticationService.token。 – CuriousAboutThings

相關問題