如何在 $scope.$on 中停止方法执行直到上传结束
How to stop method execution in $scope.$on until upload ends
我正在尝试通过点击表单保存上传一批文件。该方法应等到文件上传结束后再进行保存。现在,该方法不会在文件上传之前等待并保存。
这是我的代码
$scope.$on("formSubmitting", function (ev, args) {
$scope.uploadPendingFiles();
});
uploadPendingFiles();正在执行 XMLHttpRequest 发送。
知道怎么做吗?
编辑
按照建议,更多信息:
在表单上我可以选择一些要上传的文件,但只有在用户单击保存按钮时才会开始上传。父控制器在点击保存时广播 formSubmitting 我想显示加载 gif 直到所有文件都上传。
@TestzWCh,看来你需要使用promises。上传方法(使用 http-request)必须 return $q.defer().promise
,您将像这样使用:
FileService.uploadingMethodPromise().then(function(result){
// do your logic with result -- enable Save button, or send saving request..
})
有什么我可以为您做的吗?
因为您的任务是基于遗留代码,解决方案可以使用 preventDefault()
提交按钮事件的 click
方法。
所以我已经准备好sample,描述你的情况(如果有误会,请指出)。
关于事件传播的简单介绍,您可以阅读这篇 article。
希望对您有所帮助!
我正在尝试通过点击表单保存上传一批文件。该方法应等到文件上传结束后再进行保存。现在,该方法不会在文件上传之前等待并保存。
这是我的代码
$scope.$on("formSubmitting", function (ev, args) {
$scope.uploadPendingFiles();
});
uploadPendingFiles();正在执行 XMLHttpRequest 发送。
知道怎么做吗?
编辑
按照建议,更多信息:
在表单上我可以选择一些要上传的文件,但只有在用户单击保存按钮时才会开始上传。父控制器在点击保存时广播 formSubmitting 我想显示加载 gif 直到所有文件都上传。
@TestzWCh,看来你需要使用promises。上传方法(使用 http-request)必须 return $q.defer().promise
,您将像这样使用:
FileService.uploadingMethodPromise().then(function(result){
// do your logic with result -- enable Save button, or send saving request..
})
有什么我可以为您做的吗?
因为您的任务是基于遗留代码,解决方案可以使用 preventDefault()
提交按钮事件的 click
方法。
所以我已经准备好sample,描述你的情况(如果有误会,请指出)。
关于事件传播的简单介绍,您可以阅读这篇 article。
希望对您有所帮助!