将数据从服务传递到控制器AngularJS

时间:2016-03-03 22:41:01

标签: javascript angularjs

我有一项服务来从作为参数传递的url中获取数据。有用。 但是当我想将这些数据传递给控制器​​$ scope时,我什么都没得到



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 个答案:

答案 0 :(得分:1)

您的obtenerDatos函数没有返回任何内容 - 它只是对$http进行异步调用。尝试返回$http调用的结果(角度承诺),然后将.then处理程序附加到控制器中返回的promise:

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

另请注意,由于the former have been deprecated,我将.success().error()回调更改为.then().catch()

答案 1 :(得分:0)

谢谢!我用尼古拉斯·格拉齐亚诺的答案解决了。 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);