javascript - 如何在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
解决方案
我能想到的唯一原因是因为asynchronous
call of promises
。发生的事情是:
不知何故,您是在 的 承诺完成后设置Service2.getDetails($scope.genewtId)
的值之前调用的,因此该值仍然存在$scope.genewtId
Service1.getId("abc").then
null
尝试:
$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);
});
};
推荐阅读
- macos - qemu-img dyld:库未加载:/usr/local/opt/nettle/lib/libnettle.6.dylib
- python - 如何直接在浏览器中运行 Flask 应用程序?
- reactjs - 有什么比 refs 更适合重新渲染多个组件之一的吗?
- cpu-architecture - 处理器中除以零
- nativescript - NS6.5 (Android) 错误:无效的可见性值:[object Object]。有效值为:可见、隐藏、折叠
- reactjs - onClick 不会改变 REACTJS 中的 useState
- twilio - 使用 Twilio for WhatsApp 在 Microsoft Botframework 中发布机器人
- python - 为什么我在运行 loaddata 时会收到此错误?
- reactjs - 对所有表格页面的所有输入字段进行 React AntDesign 表单验证
- constructor - 二维数组的复制构造函数