首页 > 解决方案 > 如何在AngularJS中将数据从一个异步函数传递到另一个异步函数

问题描述

如何以角度将全局变量值从一个函数传递给另一个函数?

我有 2 个全局变量:

$scope.genewtId = null;
$scope.data1 = null;

我有 2 个角度函数,如下所示:

$scope.getID = function() {
    Service1.getId("abc").then(function(response){
        $scope.genewtId = response.data[0].Id;
        console.log($scope.genewtId);

    }, function(error){
        console.log(error.statusText);
    });
};

$scope.getDetails = function() {
    Service2.getDetails($scope.genewtId).then(function(response){
        // here response is having an error
        $scope.data1 = response.data;
        console.log($scope.data1.toString());
    }, function(error){
        console.log(error.statusText);
    });
};

当我将一个函数的值传递$scope.genewtId给另一个函数时,出现错误

消息:“无法将 'java.lang.String' 类型的值转换为所需类型 'java.lang.Integer';嵌套异常是 java.lang.NumberFormatException:对于输入字符串:“null””

但是,console.log($scope.genewtId);正在返回一个值787651,这意味着它不为空。

请建议是否可以使用$rootScope.$broadcast

标签: javascriptangularjsangular-promiseangularjs-serviceangularjs-http

解决方案


我能想到的唯一原因是因为asynchronouscall of promises。发生的事情是:

不知何故,您是在 的 承诺完成后设置Service2.getDetails($scope.genewtId)的值之前调用的,因此该值仍然存在$scope.genewtIdService1.getId("abc").thennull

尝试:

$scope.getID = function(isCalledAfterDetails) {
    Service1.getId("abc").then(function(response){
        $scope.genewtId = response.data[0].Id;
        console.log($scope.genewtId);
        if(isCalledAfterDetails && $scope.genewtId !== null){
            $scope.getDetails();
        }

    }, function(error){
        console.log(error.statusText);
    });
};

$scope.getDetails = function() {
    if($scope.genewtId === null){
        $scope.getID(true);
    }else{
        Service2.getDetails($scope.genewtId).then(function(response){
            // here response is having an error
            $scope.data1 = response.data;
            console.log($scope.data1.toString());
        }, function(error){
            console.log(error.statusText);
        });
    }

};

即使这种方法有效,我强烈建议您以更好的方式实现函数调用,因为$scope.getDetails()它依赖$scope.getID()$scope.genewtId.

如果您想就如何实现它提出建议,请使用用例和更多代码更新问题

更新

$scope.getID = function() {
    Service1.getId("abc").then(function(response){
        $scope.genewtId = response.data[0].Id;
        $scope.getDetails();
    }, function(error){
        console.log(error);
    });
};

$scope.getDetails = function() {
        Service2.getDetails($scope.genewtId).then(function(response){
            // here response is having an error
            $scope.data1 = response.data;
            console.log($scope.data1.toString());
        }, function(error){
            console.log(error.statusText);
        });        
};

使用服务

service.js中

getDetails = function(id){
    var deferred = $q.derfer();
    $http.get('/user/'+id).then(function(response){
        var newId = response.data[0].Id;
        $http.get('/user/details'+newId).then(function(details){
            deferred.resolve(details)
        })
    })      
    return deferred.promise;
}

控制器.js

$scope.getDetails = function() {
        MySvc.getDetails("abc").then(function(response){
            console.log(response) // your details here
        }, function(error){
            console.log(error.statusText);
        });        
};

推荐阅读