AngularJS - 工厂未定义?

时间:2015-08-19 20:06:01

标签: angularjs node.js bower angularjs-controller angularjs-factory

我试图将工厂注入控制器。 工厂用于管理身份验证事件。

这里有一些代码:

'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 个答案:

答案 0 :(得分:1)

API_REST&amp;内部添加缺少的factory角度常量相关性controller

<强>工厂

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

控制器也搞砸了依赖关系的排序&amp;很少依赖,如$state&amp; $location遗失了。

<强>控制器

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

您应该按照在数组中注入并在函数中使用它的顺序进行操作。

修改

似乎某些依赖项未被使用,您可以删除它们,因为它们不会被使用。 API_REST$rootScope&amp; $location