Walter Lauxmann
Walter Lauxmann

Reputation: 163

Pass data from a service to a controller AngularJS

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

Upvotes: 0

Views: 67

Answers (2)

Walter Lauxmann
Walter Lauxmann

Reputation: 163

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

Upvotes: 0

Nathan Friend
Nathan Friend

Reputation: 12834

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.

Upvotes: 1

Related Questions