我有一个异步休息电话。在调用其他所有内容后,将在最后检索响应。在进入下一步之前我需要响应。 我计划实施承诺。 在我的案例中,我不知道如何实现承诺。 我的代码:
git config --global user.name xxx
当设置$ scope.savedDesc时,只有我想继续下一步。
请建议。 提前谢谢。
答案 0 :(得分:0)
更改
if($scope.savedDesc.length <= 0 && $scope.savedDesc != null && $scope.savedDesc != 'undefined') {
access.getDesc($scope.id, function(data){
$scope.savedDesc = data;
});
}
// next step(s) here...
要
if($scope.savedDesc.length <= 0 && $scope.savedDesc != null && $scope.savedDesc != 'undefined') {
access.getDesc($scope.id, function(data){
$scope.savedDesc = data;
// next step(s) here...
});
}
答案 1 :(得分:0)
如果access
是angular $resource
,您可以执行以下操作:
function nextStep(){
//...do something with $scope.savedDesc...
}
$scope.savedDesc = access.getDesc($scope.id, nextStep);
// or
$scope.savedDesc = access.getDesc($scope.id);
//... (do something else)
$scope.savedDesc.$promise.then(nextStep);
答案 2 :(得分:0)
您可以使用callback
功能逐步执行。
if($scope.savedDesc.length <= 0 && $scope.savedDesc != null && $scope.savedDesc != 'undefined')
{
access.getDesc($scope.id, function(data){
$scope.savedDesc = data;
noteDescCall($scope.savedDesc); // call foreach inside this function with $scope.saveDesc
});
}
// your noteDescCall function which have `foreach` loop
function noteDescCall(savedDesc){
// implementation with savedDesc ...
}
答案 3 :(得分:0)
MyAsyncRestCall()
.then(function(result1) {
// you can modify `result1` before returning it if you want
return result1;
})
.then(function(result2) {
// `result2` is the data returned from the above `then` (e.g. `result1`)
// returning `result2` returns the value to the original caller
return result2;
})
.catch(function(error) {
// handle errors here if you want to
return $q.reject({ message: JSON.stringify(error) });
});
这是一篇关于承诺链的好文章:http://www.syntaxsuccess.com/viewarticle/angular-promise-chaining-explained