AngularJs - 指令和服务之间的通信

时间:2017-10-19 09:06:32

标签: angularjs file-upload angularjs-directive

我有一个指令,通过调用服务函数来处理文件上传和下载。
问题是我有一个上传文件的进度条,我希望它与我上传的文件同步,为了实现这一点,我必须得到进度事件的e.loaded值(参见下面的代码示例) )每次这个值改变。

以下是我服务的上传功能:

function upload(file, attachableId, className, appName){
            var fd = new FormData();
            fd.append("file", file);
            return $http({
                method: 'POST',
                data: fd,
                url: URL+"?attachableId="+attachableId+"&className="+className+"&appName="+appName,
                headers: {'Content-Type': undefined},
                uploadEventHandlers: {
                    progress : function(e){

                      // I want to access the value of e.loaded in my directive each time it changes 
                      // so that I can update my progress bar

                      console.log(e.loaded + " uploaded of " + e.total);
                    }
                }
            })
        }

我不知道如何在服务的指令中获取该值,因为我知道在上传按钮的ng-click中我只调用了一次服务上传功能。

任何帮助将不胜感激!

0 个答案:

没有答案