如何添加角$ HTTP事件监听器 [英] how to add Angular $http event listeners

查看:91
本文介绍了如何添加角$ HTTP事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前上传文件中的指令角与...

I'm currently uploading a file in an angular directive with...

var fd = new FormData();
fd.append("uploadedFile", scope.uploadedFile);

var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListenter("load", uploadComplete, false);
xhr.addEventListenter("error", uploadFailed, false);
xhr.addEventListenter("abort", uploadCanceled, false);
xhr.open("POST", scope.postUrl);
scope.uploadInProgress = true;
xhr.send(fd);

function uploadProgress(e){
  scope.$apply(function(){
    if(e.lengthComputable){
      scope.progress = Math.round(e.loaded * 100 / e.total);
    } else {
      scope.progress = 'unable to compute';
    }
  });
 }

 ...

可以此片段使用$ HTTP提供重构? ,我不能找出如何保持我事件监听器。

Can this snippet be refactored using $http provider? i can't figure out how to keep my event listeners.

推荐答案

简答:不久你就可以,但还没有完全按照你要求的

Short answer: soon you will be able to, but not yet exactly as you have requested.

有正在讨论几个选项打造的功能 - 揭露基地 XHR 对象,或允许回调的方法进行设置。

There are a few options being discussed to build in the functionality - exposing the base xhr object, or allowing callbacks to be set for the methods.

请参阅 https://github.com/angular/angular.js/issues/1934 专门 - 进度事件将暂时不考虑工作。

See https://github.com/angular/angular.js/issues/1934 specifically - progress events won't work for the moment.

在此期间,我会建议手动创建一个对象,并使用 $适用来更新你的范围,就像你。

In the meantime, I would recommend creating an object manually and using $apply to update your scope, just as you have.

请参阅此问题上如何使用服务来设置至少启动和停止,你可以捕捉事件的详细信息 - 但对进展没有运气

See this question for more details on how to use a service to set up at least start and stop events you can catch - but no luck on the progress.

<一个href=\"http://stackoverflow.com/questions/13746052/angularjs-need-to-fire-event-every-time-an-ajax-call-is-started\">AngularJS:需要每一个Ajax调用启动时间开火事件

这篇关于如何添加角$ HTTP事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆