0

I have a service to get data from a url passed as a parameter. It works. But When I want to pass this data to a controller $scope, I do not get anything

var app= angular.module("ReciboApp",[]);
    // -------- SERVICIOS ------------------- 
    app.service("ABMService",function($http){
        this.obtenerDatos= function(url){
            $http.get(url)
                .success(function(data) {
                    datos = eval(data);
                    console.log(datos); //[Object, Object, Object, Object, Object]
                    return datos                                                    
                })
                .error(function(data) {
                    console.log('Error: ' + data);
                });
        }
    });
// -------- CONTROLADORES -------------------
// -- Empresas --
var empresasController= function($scope, ABMService){
    var url= "modelos/empresas_json.php"
    $scope.empresas= [];
    $scope.empresas = ABMService.obtenerDatos(url);
    console.log($scope.empresas); //undefined
}
app.controller("EmpresasCtrl", empresasController);

2

2 Answers 2

1

Your obtenerDatos function doesn't return anything - it just makes an asynchronous call to $http. Try returning the result of the $http call (an angular promise) and then attach a .then handler to the returned promise in your controller:

var app= angular.module("ReciboApp",[]);
    // -------- SERVICIOS ------------------- 
    app.service("ABMService",function($http){
        this.obtenerDatos= function(url){

            // add a return statement here
            return $http.get(url)

                // change .success() to .then()
                .then(function(data) {
                    datos = eval(data);
                    console.log(datos); //[Object, Object, Object, Object, Object]
                    return datos;                                                    
                })

                // change .error() to .catch()
                .catch(function(data) {
                    console.log('Error: ' + data);
                });
        }
    });
// -------- CONTROLADORES -------------------
// -- Empresas --
var empresasController= function($scope, ABMService){
    var url= "modelos/empresas_json.php"
    $scope.empresas= [];

    // wait for the obtenerDatos() call to complete, and then
    // attach the returned data to the $scope
    ABMService.obtenerDatos(url).then(function(datos) {
        $scope.empresas = ABMService.obtenerDatos(url);
        console.log($scope.empresas); //undefined
    });
}
app.controller("EmpresasCtrl", empresasController);

Also note that I changed the .success() and .error() callbacks to .then() and .catch(), since the former have been deprecated.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for you answer, but don't show data in view (ng-repeat)
0

Thanks! I solved with a answer of Nicholas Graziano. https://stackoverflow.com/a/35783564/6015590

app.factory('MYAPI', function($http) {
    return {
        obtenerDatos: function(url) {
            return $http.get(url);
        }
    }
});
var empresasController= function($scope, MYAPI){
    var url= "modelos/empresas_json.php";
    MYAPI.obtenerDatos(url).then(function(response) {
        $scope.empresas = eval(response.data);
    }, function(error) {
        console.error(error);
});
app.controller("EmpresasCtrl", empresasController);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.