Angular 1.5.4 $http 进度事件 [英] Angular 1.5.4 $http progress event
问题描述
现在,Angular 1.5.4 终于允许您跟踪 $http 提供程序上的进度事件,但出于某种原因,我一直将 $rootScope 作为响应而不是实际进度(我将其用于上传)信息.由于缺乏示例,我在 Angular 存储库中找到了一些测试并遵循了该测试,但没有成功.
Now Angular 1.5.4 finally allows you to track progress event on $http provider but for some reason I keep getting the $rootScope as a response instead of an actual progress (I'm using it for uploads) information. Because of lack of examples I found some tests in the Angular repo and followed that but to no success.
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
In both cases it consoles $rootScope rather than the lengthComputable
推荐答案
嗯,我最终做了这样的事情,只是自己处理它,因为添加到 $http 的 XHR 事件对我不起作用.
Well I ended up doing something like this and just handle it myself as the XHR events added to $http dont work for me.
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;
这篇关于Angular 1.5.4 $http 进度事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!