私のアプリには少し問題があります。私は取得し、無名関数の外でデータ配列を返したいと思います。私は約束を使用し、私の問題はランダムな値でランダムな配列の長さを返すサービスをしようとすることです。データの取得をお待ちしておりますangularJS
私はこの問題を知らないが、私は約束を使うかどうか分からない。
getCurrentExchangeTo : function(year, month, country){
var def = $q.defer();
var numberDayPerMonth = [
31,
9,
31,
30,
31,
30,
31,
30,
31,
30,
31,
30,
];
var vm = this;
this.country = country;
this.getCurrentExchangeFor = [];
var hello = "gello"
for(var i = 0; i < numberDayPerMonth.length; i++){
if((i + 1) === month){
for(let j = 1; j < numberDayPerMonth[i]; j++){
$http.get('http://api.fixer.io/2000-02-0' + j + '?symbols=USD').then(function (success) {
let countryDay = vm.country
vm.getCurrentExchangeFor[j] = success.data.rates[countryDay];
def.resolve(getCurrentExchangeFor)
});
}
}
}
return def.promise
}
と
getCurrentExchangeService.getCurrentExchangeTo(2015, 2, 'USD').then(function (data) {
console.log(data)
});
JavaScriptを非同期..ですuはループが続くとAPIを呼び出し続けるAPIを呼び出すとき:
は、私の知る限り、次欲しいです。それぞれの呼び出しは別の時間に戻ります。したがって、あなたは乱数を得ます... – ziaulain
この権利..しかし、あなたは私の問題の結果についての考えを持っていますか? – simonmnt
return文は、ループ内のresolve文の後に来る必要があります。 –