How to pass this variable to the scope?

0
(function () {
    angular.module("Hawking").controller('loginController', function ($log, $scope, validateUser) {

        var dataUser = { email: "[email protected]", senha: "123456" };


        validateUser.getUser(dataUser).then(function(data){
            console.log(data); // ok!
            $scope.data = data;
        });


        console.log($scope.data); // undefined 
    })
        .factory('validateUser', function ($http, $q) {
            return {
                getUser: function (userInfo) {
                    var deferred = $q.defer();
                    var status = {
                            id_usuario: null,
                            permission: false
                    };
                     $http.get('/hawkingBE/api/usuarios/').then(function (response) {
                        angular.forEach(response.data, function (value, key) {
                            if (value.email == userInfo.email && value.senha == userInfo.senha) {
                                status = {
                                    id_usuario: value.id_usuario,
                                    permission: true
                                }
                            }
                        });
                    });
                    deferred.resolve(status);
                    return deferred.promise;
                }
            }
        });
})();
    
asked by anonymous 27.05.2017 / 22:31

1 answer

0

The javascript is asynchronous. In other words, in the javascript execution stack the $scope.data variable is not defined. Only then when solving the promise of the factory will it be assigned to the desired variable, ie, there is no way to pass this scope because it is not synchronous.

    
30.05.2017 / 21:25