如何在 AngularJS 中将数据从一个异步函数传递到另一个异步函数 [英] How to pass data from one asynchronous to another asynchronous function in AngularJS

查看:51
本文介绍了如何在 AngularJS 中将数据从一个异步函数传递到另一个异步函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个全局变量:

I have 2 global variables as:

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

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

I have 2 angular functions which look as below:

$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 的值从一个函数传递给另一个函数时,出现错误

When I pass value of $scope.genewtId from one function to another function, I get an error

message: "无法将类型 'java.lang.String' 的值转换为所需的类型 'java.lang.Integer';嵌套异常是 java.lang.NumberFormatException: For input string: "null""

message: "Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "null""

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

However, console.log($scope.genewtId); is returning a value 787651 which means it is not null.

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

推荐答案

两个服务的promise需要链接

修改第一个函数返回一个promise:

The promises from the two services need to be chained

Modify the first function to return a promise:

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

修改第二个函数以返回一个承诺并接受一个参数:

Modify the second function to both return a promise and accept an argument:

$scope.getDetails = function(id) {
    var genewtID = id || $scope.genewtId;
    return Service2.getDetails(genewtId).then(function(response){
        $scope.data1 = response.data;
        console.log($scope.data1.toString());
        return response.data;
    }, function(error){
        console.log(error.statusText);
        throw error;
    });
};

然后链接两个承诺:

var promise = $scope.getId();

var promise2 = promise.then(function(id) {
                   return $scope.getDetails(id);
               });

var promise2.then(function(data) {
     console.log(data);
}).catch(function(error) {
     console.log(error);
});

通过使用 getId 承诺的 .then 方法,代码在执行之前等待 id 值从服务器到达请求 getDetails.

By using the .then method of the getId promise, the code waits for the id value to arrive from the server before making the request to getDetails.

因为调用 Promise 的 .then 方法会返回一个新的派生 Promise,所以很容易创建一个 Promise 链.可以创建任意长度的链,并且由于一个承诺可以用另一个承诺来解决(这将进一步推迟其解决方案),因此可以在链中的任何点暂停/推迟承诺的解决.

Because calling the .then method of a promise returns a new derived promise, it is easily possible to create a chain of promises. It is possible to create chains of any length and since a promise can be resolved with another promise (which will defer its resolution further), it is possible to pause/defer resolution of the promises at any point in the chain.

有关详细信息,请参阅

这篇关于如何在 AngularJS 中将数据从一个异步函数传递到另一个异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆