Angular JS - 我的控制器函数在执行我的服务脚本后永远不会返回

时间:2015-06-19 14:53:07

标签: javascript angularjs angular-http-interceptors

在我的应用上登录用户的过程正在运行。它连接到服务器并返回200状态,因此它正常工作。但是在我的服务完成任务后,我的控制器没有执行它的行:

控制器

 loginService.signin(formData, function(res) {
     console.log('This message never prints');
 });

有没有人知道如何解决这个问题?也许我的拦截器参与了这种行为。这是我的脚本:

app.js

'use strict';

var app = angular.module('myapp', [
'ngRoute',
'ngStorage',
'ngCookies',
'myapp.controllers',
'myapp.services'
]);

app.config(['$routeProvider', '$httpProvider', function ($routeProvider, $httpProvider) {

    $routeProvider
        .when('/', {
            templateUrl: 'views/logueo.html',
            controller: 'LoginController'
        })
        .when('/principal', {
            templateUrl: 'views/principal.html'
        })
        .otherwise({
            redirectTo: '/'
        });
}]);

controller.js

angular.module('myapp.controllers', [])

    .controller('LoginController', 
        ['$rootScope', 
        '$scope', 
        '$http', 
        '$location', 
        '$localStorage', 
        'loginService',
        '$cookies',
        function ($rootScope, $scope, $http, $location, $localStorage, loginService, $cookies) {

        $scope.signin = function() {

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

            loginService.signin(formData, function(res) {

                // PROBLEM HERE.
                // ITS NOT EXECUTING THE REST OF THIS LINES.

                // Usuario incorecto.
                if (res.type == false) {
                    console.log("FALSE " + res.data);

                } 
                // Usuario correcto.
                else {
                    window.location = "#/principal.html"; 
                }

            }, function() {
                $rootScope.error = "Error en el logueo del usuario";
            });
        }
    }]);

service.js

'use strict';

angular.module('myapp.services', [])
.factory('loginService', ['$http', '$localStorage', 'tokenStorage',
        function ($http, $localStorage, tokenStorage) {
        // THIS IS EXECUTING VERY WELL
        return {
            signin: function(data, success, error) {

                $http.post(
                        'http://myweb.com/api' + '/login', 
                        data
                    ).success( function (result, status, headers) {
                        console.log("logueado");

                    }).error(function (data, status, headers, config) {
                        console.log("error. " + data);
                    })
            }
        };

    }]);

1 个答案:

答案 0 :(得分:0)

您没有调用signin函数传递的成功函数:

signin: function(data, success, error) {

            $http.post(
                    'http://myweb.com/api' + '/login', 
                    data
                ).success( function (result, status, headers) {
                    success(result);
                    console.log("logueado");
                    tokenStorage.store(headers('X-AUTH-TOKEN'));

                }).error(function (data, status, headers, config) {
                    console.log("error. " + data);
                })
        }