我怎样才能确保complete()
无论使用 Angular.js 提供的 promise API 调用 $http 的结果如何,函数都会运行吗?
$http({
method: 'POST',
url: submitUrl,
data: $scope.data
})
.success(function(data) {
// execute this code on success
})
.error(function(data) {
// execute this code on error
})
.complete(function() {
// execute this code regardless of outcome
});
一旦请求完成,人们可以使用它来隐藏 AJAX 微调器图标。无论请求结果如何,您都希望隐藏微调器。
最佳答案
我不是 Angular.js 世界上最伟大的专家,但我知道你可以这样做:
whatever.then(function() {
// success code here
}, function() {
// error code here
return true; // return anything that's not undefined (and not a `throw()`) to force the chain down the success path at the following then().
}).then(function() {
// "complete" code here
});
您基本上被迫从一个或多个
.then()
中设计出一些东西,这是 $q promise 的唯一方法。
关于ajax - Angular.js $http 请求后,无论promise成功还是失败,都调用完整的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17181508/