2つのリクエストが完了した後に関数を呼び出す必要があります。だから私は$ q.allを使ってこれを試みます。私はAngularのサービスを持っています。それはsubmitFormを呼び出す前に2つの別の要求を行う必要があり、応答後にフォームを送信するための投稿要求を行います。 $q.all
で
submitForm : function(form, params) {
var self = this;
var callback = function() {
// this is submit post req
};
angular.forEach(form, function(item) {
// I search tables (type of field) and then send req for saving tables on backend
self.uploadTables(item)
})
this.uploadFiles(params).then(callback)
}
uploadFiles : function(params) {
// this func upload files before submit
}
uploadTables : function(params) {
// for uploading tables I have another service on backend, so I need another request, before submit
}
私は$q.all(this.uploadFiles, this.uploadTables)
ようにそれを呼び出す必要が推測:それはこのようになりますか?しかし、私はそれを行うことはできませんアップロードTables私はforEachを呼び出します。 uploadFilesとuploadTablesの完了後にコールバック関数を呼び出すにはどうすればよいですか?
あなたは 'forEach'を使用しません。代わりに 'map'を使います。 – Bergi