这是我的代码:
function myobject(name, anotherArray){
this.name = name; this.anotherArray = anotherArray;
}
//$scope.myObjects is an array of myObject
function pMap(name, promise){
this.name= name;
this.promise = promise;
};
var getAllPromises = function(){
var promises = [];
angular.forEach($scope.myobjects, function(myobject) {
urlPath = <someurl>+myobject.name;
var promise = $http({
url : urlPath,
method: 'GET'
});
promises.push(new pMap(myobject.name, promise));
});
return $q.all(promises);
};
现在当我使用.then在一个单独的函数中接受如下的每个promise时,我不知道如何访问上面服务返回的json?
getAllPromises.then(function(pData){
angular.forEach(pData, function(pMap){
var name = pMap.name;
var responseArray = promiseMap.<?> --> how do I access the json returned by the service here?
<I need to assign $scope.myobjects.anotherArray to data returned by the promise but this has to be done using the name attribute of each myobject>
});
});
任何想法如何做到这一点? 新的承诺模式和angularjs一般
答案 0 :(得分:1)
你可以这样做:
resolvedPromises
$scope.myobjects
这里是一个数组,索引将与你推送它们相同。你可以通过索引访问每个其他的promise。
如果你不打算使用myobject
,我不确定为什么你会循环{{1}}。