1

私は工場に注入したコントローラを持っていますが、その工場のメソッドを呼び出すと、定義されていません。私がここで間違っていることを確かめない。どんな助けもありがとう。コントローラに注入されるとサービスが未定義

工場:

(function(){ 
    'use strict'; 

    // Declare factory and add it 'HomeAutomation' namespace. 
    angular.module('HomeAutomation').factory('AuthenticationService', ['$http','$localStorage', '$window', function($http, $localStorage, $window){ 
    var service = {}; 


    service.login = Login; 
    service.logout = Logout; 
    service.parseJWT = parseJWT; 
    service.loginStatus = loginStatus; 

    return service; 


    function Login(email, password, callback){ 
     $http.post('api/user/login', {email: email, password: password}) 
      .success(function(res){ 
       // Login successful if there is a token in the response. 
       if(res.token){ 
        // store username and token in local storage to keep user logged in between page refreshes 
        $localStorage.currentUser = { email: email, token: res.token }; 

        // add jwt token to auth header for all requests made by the $http service 
        $http.defaults.headers.common.Authorization = 'Bearer ' + res.token; 

        callback(true); 
       }else{ 
        callback(res); 
       } 
      }).error(function(err){ 
      console.log(err); 
     }); 
    } 

    function Logout(){ 
     $localStorage.currrntUser 
    } 

    function parseJWT(token){ 
     var base64URL, base64; 

     base64URL = token.split('.')[1]; 
     base64 = base64URL.replace('-', '+').replace('_', '/'); 

     console.log(JSON.parse($window.atob(base64))); 
    } 

    function loginStatus(){ 
     if($localStorage.currentUser){ 
      return true; 
     }else{ 
      return false; 
     } 
    } 
}]);}()); 

コントローラー:

(function(){ 
angular.module('HomeAutomation') 
    .controller('loginController', ['$scope', '$location', 'AuthenticationService', function($scope, $location, $localStorage, AuthenticationService){ 
     $scope.isLoggedIn = AuthenticationService.logout(); 

     $scope.logUserIn = function(){ 
      AuthenticationService.login($scope.login.email, $scope.login.password, function(result){ 
       if(result === true){ 
        $location.path('/'); 
       }else{ 
        console.log(result); 
       } 
      }); 
     }; 

     $scope.logUserOut = function(){ 
      AuthenticationService.logOut(); 
     } 
    }]);}()); 

これはERRを引き起こしている行です。

$scope.isLoggedIn = AuthenticationService.logout(); 

どうやら "AuthenticationServiceは" 未定義です。理由は分かりません。

ありがとうございます。

答えて

2

あなたはdepedency配列とめちゃくちゃ、あなたは$localStorageはDI配列に注入されていない&コントローラにはどこにも使用されていないので、コントローラの工場機能から$localStorageを削除する必要があります。

.controller('loginController', ['$scope', '$location', 'AuthenticationService', 
    function($scope, $location, AuthenticationService){ 
       //^^^^^^^^^^removed $localStorage dependency from here 

注:は、必ずコントローラ機能で使用すると、DI配列内の任意の依存関係を注入するとき、彼らは同じ順序で使用すべきであることを確認してください

+0

HA、良いキャッチ....私はばかだ。 –

0

注入は良くありません。

.controller('loginController', ['$scope', '$location', 'AuthenticationService', function($scope, $location, $localStorage, AuthenticationService){ 

これで試す:

.controller('loginController', ['$scope', '$location', '$localStorage', 'AuthenticationService', function($scope, $location, $localStorage, AuthenticationService){ 

私はあなたが使用しているビルダー知らないが、Gulpと、たとえば、あなたはあなただけ書くことができるようにあなたのための仕事をすることgulp-ng-annotateを使用することができます。配列のない

.controller('loginController', function($scope, $location, $localStorage, AuthenticationService){ 

ng-annotateは残りの部分を処理します。

+0

私はガルプを使用します。私はそれを試してみる。ありがとう。 –

関連する問題