我正在使用REST,它提供了非嵌套的资源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。
我想知道我是不正确地使用了承诺的链,还是我使用了错误的元素
发布于 2015-10-19 09:42:35
你没有正确地把你的承诺捆绑在一起。对于每个块,立即向服务器发送另一个请求。
使用$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){...
发布于 2015-10-19 09:38:23
https://stackoverflow.com/questions/33211057
复制相似问题