提前致谢。我很新并且坚持AngularJs的承诺。 原始代码是:
$scope.fillInRO = function (data){
var defer = $q.defer();
$scope.receive_order = data.receive_order;
var orders = _.map(data.receive_order.purchase_orders,function (value) {
return {purchase_order:value};
});
$scope.fillInPOInitial(orders).then(
function (){
defer.resolve();
}
)
return defer.promise;
}
现在我需要添加另一个Promise.all
let promises = ids.map((id,index) => {
PurchaseOrderFactory.getPurchaseOrder(id,store_id).then(function(res){
$scope.receive_order.receive_order_items[index].receive_order_item.display_unit = res.purchase_items[index].display_unit
})
})
通过console.log,我知道res是:
res = {
purchase_items:[
{display_unit: "kg"},
{display_unit: "g"},
]
}
在请求之前,左侧值为:
$scope.receive_order.receive_order_items = [
{receive_order_item:{display_unit: null}},
{receive_order_item:{display_unit: null}},
]
请求后,值为:
$scope.receive_order.receive_order_items = [
{receive_order_item:{display_unit: "kg"}},
{receive_order_item:{display_unit: null}},
]
但我想要的是:
$scope.receive_order.receive_order_items = [
{receive_order_item:{display_unit: "kg"}},
{receive_order_item:{display_unit: "g"}}, // here the diff
]
我不知道如何在promise中正确地嵌套promise.all,我试着这样做:
$q.all(promises).then(() =>{
$scope.fillInPOInitial(orders).then(
function (){
defer.resolve()
}
)
})
似乎没有区别,我仍然得到第二个值为null:
$scope.receive_order.receive_order_items = [
{receive_order_item:{display_unit: "kg"}},
{receive_order_item:{display_unit: null}},
]
有什么想法吗?
答案 0 :(得分:1)
感谢@mzulch,我发现AngularJs Promise没有任何问题。错误发生在对ids.map的调用中 添加另一个循环并解决问题:
let promises = ids.map((id) => {
PurchaseOrderFactory.getPurchaseOrder(id,store_id).then(function(res){
res.purchase_items.map((purchase_item, index) => {
data.receive_order.receive_order_items[index].receive_order_item.display_unit = purchase_item.display_unit
})
})
})
答案 1 :(得分:0)
let promises = ids.map((id,index) => {
return PurchaseOrderFactory.getPurchaseOrder(id,store_id).then(function(res){
$scope.receive_order.receive_order_items[index].receive_order_item.display_unit = res.purchase_items[index].display_unit
})
})
然后将其传递给$q.all(promises )