ng-csv $q.all 将响应添加到列表 angular

ng-csv $q.all add response to list angular

我正在为我的 angular 项目使用 ng-csv 中的插件。我正在遍历一个数组并调用 $http 获取 returns 我的 csv 文件的一些信息。 我目前有 1 个结果见下文。

var deferred = $q.defer();
$http({
    method: 'get',
    url: "[endpoint]user/212121"                                                
 }).success(function(data, status, headers, config) {
    var ur = data;
     deferred.resolve([{a:ur.accountId, b:ur.firstName, c:ur.email}]);
    })
return deferred.promise; 

这将用一条记录填充 csv。然而,我正在努力让它与 $q.all 一起工作。

var queue=[];                    
$scope.testing.forEach(function(userjson){
 queue.push($http({
            method: 'get',
            url: "[endpoint]"+userjson.userId                                               
         }).then(function(response) {
             var ur = response.data;
                $scope.output.push({a:ur.accountId, b:ur.firstName, c:ur.email});
            }));
})

$q.all(queue).then(function(response) {
  return $scope.output;
 });

 return $q.defer();

当我调试 queue 时,它充满了保存 $http 数据的承诺对象。同样 return $scope.output; 包含所有结果。 ng-csv好像没有解读出来,感觉接近了

如有任何帮助,我们将不胜感激。

更新你的函数:

$http({
        method: 'get',
        url: "[endpoint]"+userjson.userId                                               
     }).then(function(response) {
         return {a:response.accountId, b:response.firstName, c:response.email};
        })

和:

$q.all(queue).then(function(response) {
   return response;
});

将是return每个请求的所有响应的数组。

像这样写你的延迟:

function getData() {
var queue=[];
var defered = $q.defer();
$scope.testing.forEach(function(userjson){
  queue.push($http({
        method: 'get',
        url: "[endpoint]"+userjson.userId                                               
     }).then(function(response) {
         return {a:response.accountId, b:response.firstName, c:response.email};
        }));
})

$q.all(queue).then(function(response) {
  return defered.resolve(response);
});
return defered.promise;
}

并像这样使用它:

getData().then(function(data){ /*csv data is in data */ })