[英]Chaining ngResource promises
我使用REST Api提供非嵌套資源API。 這導致了鏈接的電話,我想用承諾做。 我使用角度的ngResource,我有問題鏈接調用。 這個想法首先得到一個活躍元素的描述。 在這里我要求一個JSON,響應如下:
{id : 0, block : [0,3,4]}
獲得此信息后,我嘗試獲取有關塊的數據。 實現如下:
Element.get({'id':state.elementID}).$promise.then( function(element) {
// Element hast block entry with array of belonging blockIDs
angular.forEach(element.block, function(blockId){
// Get all the Blocks, that have the defined ID ( Foreign key)
return Block.get({'id':blockId}).$promise;
});
}).then(function(block){
// Create an element and ADD it to the model
var uiElem = new UIElem(block.id, "#",block.name, "block");
$scope.list.push(uiElem);
angular.forEach(block.parameter, function(element){
/// Chain other calls...
.......
});
})
第二個然后獲得未定義塊的問題,盡管GET調用從服務器獲得正確的JSON。
我想知道我是否正在使用錯誤的鏈接或我使用錯誤的元素
你沒有正確地鏈接你的承諾。 對於每個塊,您立即向服務器發送另一個請求。
使用$ q.all進行鏈接:
// Element hast block entry with array of belonging blockIDs
return $q.all(element.block.map(function(blockId){
// Get all the Blocks, that have the defined ID ( Foreign key)
return Block.get({'id':blockId}).$promise;
}));
這應該在這里為你提供結果塊的數組: }).then(function(blocks){...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.