Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 角度$q。所有使用多层角度重复_Angularjs_Promise - Fatal编程技术网

Angularjs 角度$q。所有使用多层角度重复

Angularjs 角度$q。所有使用多层角度重复,angularjs,promise,Angularjs,Promise,我有这段代码 var final=[]; $http.get('/Scenarios/List/'+ id).success(function(resp){ angular.forEach(resp, function(value, key){ var scenario ={}; scenario.data=[]; angular.forEach(value.samples, function(b, i){ $ht

我有这段代码

var final=[];
$http.get('/Scenarios/List/'+ id).success(function(resp){
    angular.forEach(resp, function(value, key){
        var scenario ={};
        scenario.data=[];

        angular.forEach(value.samples, function(b, i){
            $http.get('/sample/One/'+ b.id).then(function(result){
                var id= result.temp_id;
                var temp ={};
                $http.get('/a/list/'+ id).then(function(result){
                    temp.a=result;
                });
                $http.get('/b/list/'+ id).then(function(result){
                    temp.b-resutl;
                });
                scenario.data.push(temp);
            })
        });

        final.push(scenario);

    });          

}); 

console.log(final); //no value

基本上,当我试图获取“最终”数据进行进一步解析时,我发现它是空的。我认为问题可能是由于缺少$q.all的用法,但我在网上查阅了许多教程,我无法找到$q.all的正确用法来解决我重复使用angular.forEach的问题,在我的例子中,有两种用法。有什么想法吗?

我想这样解决它,使用
$q。正如问题中正确提到的那样,所有的
都是您缺少的

var final = [];

$http.get('/Scenarios/List/'+ id).success(function(resp){
    processSamples(resp).then(function(final) {
        console.log(final)
    })
});  

function processSamples(resp) {
    var deferred = $q.defer();
    angular.forEach(resp, function(value, key){
        var scenario = {};
        var promises = [];
        scenario.data = [];

        angular.forEach(value.samples, function(b, i){
            promises.push($http.get('/sample/One/'+ b.id));
        });

        $q.all(promises).then(function(result) {
            angular.forEach(result, function(res, index) {
                var id= res.temp_id;
                var temp = {};
                var childpromises = [];
                childpromises.push($http.get('/a/list/'+ id));
                childpromises.push($http.get('/b/list/'+ id));
                $q.all(childpromises).then(function(res) {
                    temp.a = res[0];
                    temp.b = res[1];
                    scenario.data.push(temp);
                    if(result.length === index + 1) {
                        final.push(scenario);
                        if(resp.length === key + 1) {
                            deferred.resolve(final);
                        }
                    }
                })
            })
        })
    });
    return deferred.promise;
}

请注意,一旦最上面的循环完成,它将如何解析返回的承诺。此外,由于没有提供可验证的示例,我可能会留下一些小错误,但至少应该给出一个好主意。

您能显示您的响应数据
resp
?两个或三个项目{samples:[{description:'abac',id:1},{description:'ac',id:3}],id:3}谢谢,我可以说这个答案改变了我的很多理解,但是你能解释一下(result.length==index+1){final.push(场景);if(resp.length==key+1){延迟.解决(最终);}@onegun这可能不是最好的方法。但是由于在
forEach
内部,我们有异步代码,我们不知道何时返回API调用。因此,这些条件旨在分别为较低和较高的
forEach
找到循环的最后一次迭代,并将数据推送到相关的位置variables@onegun当我们有两个循环时在第一次迭代中,我们用最终结果解决问题,最终结果将直接传递给
。然后我们的函数calli的
一点一点地跟踪代码,但我认为这部分不起作用,任何控制台。放入此条件中的write不会触发。我认为问题需要更改为if(res.length==key+1),而不是if(resp.length==key+1),现在都可以工作了,谢谢