私はこのコードを持っています
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 の使用法が欠落していることが原因で問題が発生している可能性があると思いますが、オンラインで多くのチュートリアルをチェックしましたが、私の場合、angular.forEach の繰り返し使用を解決するための $q.all の適切な使用法を理解できません。 、 二つあります。何かご意見は?
このように解決したいの$q.all
ですが、質問で正しく述べられているように、あなたが欠けていたものを使用しています。
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;
}
最上位のループが完了すると、返された promise がどのように解決されるかに注目してください。また、検証可能な例が提供されていなかったため、マイナーな間違いを残した可能性がありますが、少なくとも良いアイデアが得られるはずです。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加