现在,Angular 1.5.4最终允许您跟踪$ http提供程序上的进度事件,但是由于某些原因,我一直将$ rootScope作为响应而不是实际进度(我将其用于上传)信息。由于缺少示例,我在Angular回购中找到了一些测试,但后来没有成功。
restClientInstance.post = function (requestParams) { var postParams = { method: "POST", url: API_URL + requestParams.url, headers: requestParams.headers, data: requestParams.data, eventHandlers: { progress: function (c) { console.log(c); } }, uploadEventHandlers: { progress: function (e) { console.log(e); } } }; var promise = $http(postParams) $rootScope.$apply(); return promise; };
在这两种情况下,它都控制台$ rootScope而不是 lengthComputable
好吧,我最终做了这样的事情,只是自己处理了,因为添加到$ http的XHR事件对我不起作用。
var xhttp = new XMLHttpRequest(); var promise = $q.defer(); xhttp.upload.addEventListener("progress",function (e) { promise.notify(e); }); xhttp.upload.addEventListener("load",function (e) { promise.resolve(e); }); xhttp.upload.addEventListener("error",function (e) { promise.reject(e); }); xhttp.open("post",API_URL + requestParams.url,true); xhttp.send(requestParams.data); return promise.promise;