私は別のJSファイルにサービスを持っています。このサービスは、Webから必要なデータをロードするためのメソッドを保持するOOPクラスのようなものです。Q.all.thenは完了を待たずに
:
が、私は実際に私は3種類のデータをロードし、そのデータが取得され刚性待つようにJSの流れを強制したい、これらのメソッド」を呼び出して、私のメインのJSファイル内のデータを取得したい、ここに私のコードですservices.js
// My 'Class' to load data from the web server
myApp.factory("LoadData", ["_gl", function (_gl) {
return {
GetUsers: function ($http) {
$http({
method: 'POST',
url: 'http://localhost/dgis/ps/select.php',
data: { "action": "GetUsers" }
}).then(function successCallback(response) {
// Save the response JSON object to my global objects
_gl.myUsers = response.data;
}, function errorCallback(response) {
console.log("GetUsersError:" + response);
});
},
GetObGroups: function ($http) {
$http({
method: 'POST',
url: 'http://localhost/dgis/ps/select.php',
data: { "action": "GetObGroups" }
}).then(function successCallback(response) {
// Save the response JSON object to my global objects
// This code fills array because it iterates through it
angular.forEach(response.data, function (value, key) {
_gl.myObGroups.push(value)
});
}, function errorCallback(response) {
console.log("GetObGroups:" + response);
});
},
GetObjects: function ($http) {
$http({
method: 'POST',
url: 'http://localhost/dgis/ps/select.php',
data: { "action": "GetObjects" }
}).then(function successCallback(response) {
_gl.myObjects = response.data;
}, function errorCallback(response) {
console.log("GetObjectsError:" + response);
});
}
}
}]);
// My global variables
myApp.factory('_gl', function() {
return {
myUsers: [],
myOrganisations: [],
myObGroups: [],
myObjects: []
}
});
script.js
Q.all([LoadData.GetUsers($http), LoadData.GetObGroups($http), LoadData.GetObjects($http)]).then(function() {
console.log(_gl.myUsers);
console.log(_gl.myObGroups);
console.log(_gl.myObjects);
});
問題は、すべてのhttp要求がデータを取得するまでQ.all
が待たずに、それが起こる前にthen
の呼び出しを評価することです。確かに、私はいくつかのタイマーを使用し、ちょっと待つことができますが、私はそれを行うためのより適切な方法が必要です、あなたの知識と共有してください。
forEach
を私のthen
で取得すると、次にすべての配列が空になりますが、他の配列は空であり、なぜそれが起こるのかを知りたいのです。
ありがとうございます。
[MCVE](http://stackoverflow.com/help/mcve) –