如何在AngularJS中将数据从一个异步传递到另一个异步函数 [英] How to pass data from one asynchronous to another asynchronous function in 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
消息:无法将类型'java.lang.String'的值转换为所需的类型'java.lang.Integer';嵌套异常为java.lang.NumberFormatException:对于输入字符串:"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
推荐答案
两个服务的承诺必须链接
修改第一个函数以返回承诺:
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;
});
};
修改第二个函数以既返回promise又接受参数:
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
方法,代码在向getDetails
发出请求之前会等待id
值从服务器到达.
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的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 $q Service API Reference - Chaining Promises
- You're Missing the Point of Promises
这篇关于如何在AngularJS中将数据从一个异步传递到另一个异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!