我有兩個javscript async $ http函數:我正在使用角度js以嵌套方式動態創建表。
我想要某種方式來同步執行這些功能。
現在,j循環只對resultB的初始值執行。一旦表被編譯,然後fuctB被執行爲i的所有值。
$scope.funcA = function() {
$http({
method : 'GET',
url : url,
}).then(function successCallback(response) {
$scope.resultA = response.data;
//process based on $scope.resultA
for (var i = 0; i < $scope.resultA .length; i++){
$scope.funcB($scope.resultA[i][0]);
for(j=0; j<$scope.resultB .length; j++){
//process based on $scope.resultB
}
}
$compile(/* document element*/);
}, function errorCallback(response) {
console.log(response.statusText);
});
}
$scope.funcB = function(k){
$http({
method : 'GET',
url : url+k
data: k ,
}).then(function successCallback(response) {
return $scope.resultB = response.data;
}, function errorCallback(response) {
console.log(response.statusText);
});
}
更新與完整的代碼。不能診斷你正在嘗試做什麼.. – Aravind
更新了問題 – rtk
已清理的代碼使其更具可讀性。您已經定義了此代碼的異步預期操作。你能否以同樣的方式定義同步(細節有幫助)? – rfornal