1

我正试图将一个工厂注入控制器。 该工厂用于管理认证事件。AngularJS - 工厂undefined?

下面是一些代码:

'use strict'; 

/** 
* @ngdoc function 
* @name LoginCtrl 
* @module triAngularAuthentication 
* @kind function 
* 
* @description 
* 
* Handles login form submission and response 
*/ 
angular.module('triAngularAuthentication') 
.factory('Auth', ['$http', 'localStorageService', function ($http, localStorageService, API_REST) { 
     function urlBase64Decode(str) { 
      var output = str.replace('-', '+').replace('_', '/'); 
      switch (output.length % 4) { 
       case 0: 
        break; 
       case 2: 
        output += '=='; 
        break; 
       case 3: 
        output += '='; 
        break; 
       default: 
        throw 'Illegal base64url string!'; 
      } 
      return window.atob(output); 
     } 

     function getClaimsFromToken() { 

      var token = localStorageService.get('token'); 
      var user = {}; 

      if(token !== null){ 
       if (typeof token !== 'undefined') { 
        var encoded = token.split('.')[1]; 
        user = JSON.parse(urlBase64Decode(encoded)); 
       } 
      } 
      return user; 
     } 

     var tokenClaims = getClaimsFromToken(); 

     return { 
      signup: function (data, success, error) { 
       $http.post(API_REST + '/signup', data).success(success).error(error) 
      }, 
      signin: function (data, success, error) { 
       $http.post(API_REST + '/login', data).success(success).error(error) 
      }, 
      logout: function (success) { 
       tokenClaims = {}; 
       localStorageService.remove('token'); 
       success(); 
      }, 
      getTokenClaims: function() { 
       return tokenClaims; 
      } 
     }; 
    } 
]) 
.controller('LoginController', ['$rootScope', '$scope', '$location', 'Auth', function ($scope, $state, API_REST, localStorageService, $rootScope, Auth) { 
    function successAuth(res) { 
     localStorageService.token = res.token; 
     $state.go('admin-panel.default.dashboard-analytics'); 
    } 
    // create blank user variable for login form 
    $scope.user = { 
     email: '', 
     password: '' 
    }; 

    $scope.socialLogins = [{ 
     icon: 'fa-twitter', 
     color: '#5bc0de', 
     url: '#' 
    },{ 
     icon: 'fa-facebook', 
     color: '#337ab7', 
     url: '#' 
    },{ 
     icon: 'fa-google-plus', 
     color: '#e05d6f', 
     url: '#' 
    },{ 
     icon: 'fa-linkedin', 
     color: '#337ab7', 
     url: '#' 
    }]; 

    // controller to handle login check 
    $scope.loginClick = function() { 

     Auth.signin($scope.user, successAuth, function(){ 
      console.log('Error al loguear'); 
     }); 
    }; 
}]); 

但是,当我执行loginClick,我得到这个控制台错误消息:

TypeError: Cannot read property 'signin' of undefined 
    at Scope.$scope.loginClick (http://localhost:3000/app/authentication/login/login-controller.js:98:13) 
    at fn (eval at <anonymous> (http://localhost:3000/bower_components/angular/angular.js:13036:15), <anonymous>:4:221) 
    at http://localhost:3000/bower_components/angular-touch/angular-touch.js:477:9 
    at Scope.$eval (http://localhost:3000/bower_components/angular/angular.js:15719:28) 
    at Scope.$apply (http://localhost:3000/bower_components/angular/angular.js:15818:23) 
    at HTMLButtonElement.<anonymous> (http://localhost:3000/bower_components/angular-touch/angular-touch.js:476:13) 
    at HTMLButtonElement.jQuery.event.dispatch (http://localhost:3000/bower_components/jquery/dist/jquery.js:4435:9) 
    at HTMLButtonElement.elemData.handle (http://localhost:3000/bower_components/jquery/dist/jquery.js:4121:28) 

我新的角度,我想不出我什么做错了。

回答

1

添加缺少的两个factory & controller

API_REST角不变依赖性厂

.factory('Auth', ['$http', 'localStorageService', 'API_REST', 
    function ($http, localStorageService, API_REST) { 

控制器也搞砸了与依赖&一些依赖像$state & $location缺少排序。

控制器

.controller('LoginController', ['$rootScope', '$scope', '$state', 'API_REST', 'localStorageService', '$location', 'Auth', 
    function ($rootScope, $scope, $state, API_REST, localStorageService, $location, Auth) { 

您应该遵循的顺序,你在数组和函数中使用它注入。

编辑

好像有些依赖并没有得到使用,你可以因为他们没有被使用删除它们。 API_REST$rootScope & $location

+0

API_REST是主要的应用模块 – Pablo

+0

定义@Pablo你需要注入其采取使用它.. –

+0

但是,为什么加上'API_REST'控制器恒定?没有任何意义。 – BastianW