Angular JS - My controller function its never return after execute my service script

142 views Asked by At

The process to login a user on my app is working. It is connecting to a server and this is returned a 200 Status, so it's working fine. But after my service finish with its task, my controller is not executing its lines:

Controller:

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

Does anyone have an idea about how to resolve this? Maybe my interceptor is involved in this behavior. Here my scripts:

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

There are 1 answers

0
Emanuel Ralha On BEST ANSWER

You are not calling the success function passed by signin function:

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);
                })
        }