Angular - TypeError:XX不是函数

时间:2015-06-16 19:28:24

标签: javascript angularjs angularjs-scope angularjs-service angularjs-factory

也许我错过了某种属性,但我跟着这个project并且我在控制器中收到了这个错误。

TypeError: loginService.signin is not a function

这是我的controller.js

angular.module('appcontrollers', []).controller('LoginController', ['$rootScope', '$scope', '$http', '$location', '$localStorage', 'loginService',  
        function ($rootScope, $scope, $http, loginService) {

        $scope.signin = function() {

            console.log("username: " + $scope.username);
            console.log("pass: " + $scope.password);

            var formData = {
                username: $scope.username,
                password: $scope.password
            };

            // ERROR IN THIS LINE
            loginService.signin(formData, function(res) {
                console.log("asdf");
                if (res.type == false) {
                    console.log(res.data);
                } else {
                    $localStorage.token = res.data.token;
                    console.log("Window location /");
                }
            }, function() {
                $rootScope.error = "Error en el logueo del usuario";
            });

            // Setting Token:
            $scope.token = $localStorage.token;
        }

    }])

这是我的service.js

'use strict';

angular.module('app-services', [])
.factory('loginService', ['$http', '$localStorage', function ($http, $localStorage) {
    console.log('services - loginService');

    var baseUrl = "http://angular-restful-auth.herokuapp.com";

    function changeUser(user) {
        console.log('1');
        angular.extend(currentUser, user);
    }

    function urlBase64Decode(str) {
        console.log('2');
        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 getUserFromToken() {
        console.log('3');
        var token = $localStorage.token;
        var user = {};
        if (typeof token !== 'undefined') {
            var encoded = token.split('.')[1];
            user = JSON.parse(urlBase64Decode(encoded));
        }
        return user;
    }

    // Set user token.
    var currentUser = getUserFromToken();

    return {
        save: function(data, success, error) {
            $http.post(baseUrl + '/signin', data).success(success).error(error)
        },
        signin: function(data, success, error) {
            $http.post(baseUrl + '/authenticate', data).success(success).error(error)
        },
        me: function(success, error) {
            $http.get(baseUrl + '/me').success(success).error(error)
        },
        logout: function(success) {
            changeUser({});
            delete $localStorage.token;
            success();
        }
    };
}

]);

我的应用程序一直有效,直到用户从表单中按下名为signin()的提交按钮。然后我在控制台中使用正确的数据获得了这两行

>> username: somename
>> pass: somepassword

之后出现错误。任何人都可以帮助我传递这个signin()函数吗?

1 个答案:

答案 0 :(得分:21)

你正在搞乱依赖注入数组,当你在函数中使用它时,它应遵循正确的依赖顺序。

<强>代码

angular.module('appcontrollers', []).controller('LoginController', ['$rootScope', '$scope', '$http', '$location', '$localStorage', 'loginService',  
        function ($rootScope, $scope, $http, $location, $localStorage, loginService) {