gpt4 book ai didi

angularjs - 使用 Angular $ q.all处理错误

转载 作者:行者123 更新时间:2023-12-03 07:54:11 24 4
gpt4 key购买 nike

我有一个 Angular 应用程序,向用户显示可编辑的网格。允许用户编辑网格中的多行,然后一次保存所有这些更改。当他们这样做时,我正在使用$q.all将所有更新调用并行发送到API。

我希望能够为每次调用失败时向用户显示错误,其中包含有关未正确保存的对象的某些信息,但是我不知道如何从错误处理程序中获取该信息。

var ops = []

_.each($scope.items, function (item) {
if(item.Modified)
ops.push(dataService.update(item.itemID, item.otherField))
})

$q.all(ops)
.then(function (repsonse) {
//success
},
function (response) {
//in here I want to output the itemID and otherField values for the item(s) that failed
})

通过API调用发送的每个项目都有一些属性( itemIDotherField)。我希望这些值包含在给用户的错误消息中。

是否可以使用q.all还是必须使用其他方法?

最佳答案

如果任何操作在任何时间失败,$q.all将触发错误回调,但是在您的情况下,您只想记录哪些操作(如果有)失败而不会触发任何其他错误。我将使用.catch的return promise:

ops.push(dataService.update(item.itemID, item.otherField))
.catch(function () {
return {
status: "failed",
id: item.itemID
};
});

然后,在 $q.all的回调中,您可以遍历响应并检查 .status == "failed"并检查那些ID。

关于angularjs - 使用 Angular $ q.all处理错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31141598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com