admin管理员组文章数量:1327945
I have an angularjs application with a file upload screen.
In order to monitor progress of file upload, i have registered 'progress' event handler as follows:
xhr.upload.addEventListener("progress", uploadProgress, false);
My Function:
function uploadProgress(evt) {
$scope.$apply(function(){
if (evt.lengthComputable) {
$scope.data.progress[0] = evt.loaded;
} else {
$scope.data.progress = 0;
}
})
}
I need to pass one more parameter to uploadProgress method. How can i achieve that?
Essentially an extra parameter say 'i' like this:
xhr.upload.addEventListener("progress", uploadProgress(i), false);
I have an angularjs application with a file upload screen.
In order to monitor progress of file upload, i have registered 'progress' event handler as follows:
xhr.upload.addEventListener("progress", uploadProgress, false);
My Function:
function uploadProgress(evt) {
$scope.$apply(function(){
if (evt.lengthComputable) {
$scope.data.progress[0] = evt.loaded;
} else {
$scope.data.progress = 0;
}
})
}
I need to pass one more parameter to uploadProgress method. How can i achieve that?
Essentially an extra parameter say 'i' like this:
xhr.upload.addEventListener("progress", uploadProgress(i), false);
Share
Improve this question
asked Apr 30, 2014 at 13:27
JasperJasper
8,70533 gold badges96 silver badges135 bronze badges
1
-
1
You mean like calling
uploadProgress(i, evt)
instead ofuploadProgress(evt)
? – gkalpak Commented Apr 30, 2014 at 13:33
1 Answer
Reset to default 9This is not Angular-specific.
You can use standard JS's bind()
:
function uploadProgress(i, evt) {
...
xhr.upload.addEventListener("progress", uploadProgress.bind(null, i), false);
You can use Angular's angular.bind()
:
function uploadProgress(i, evt) {
...
xhr.upload.addEventListener("progress", angular.bind(null, uploadProgress, i), false);
Finally, you could also use a plain old closure (it is more code, but has the benefit of allowing you to specify arbitrary argument order):
function uploadProgress(evt, i) {
...
var j = i; // because `i` might have changed by the time
// our callback is called, we need the current value
xhr.upload.addEventListener("progress", function (evt) {
uploadProgress(evt, j);
}, false);
本文标签: javascriptPassing parameter to XMLHttpRequest eventListener functionStack Overflow
版权声明:本文标题:javascript - Passing parameter to XMLHttpRequest eventListener function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742223936a2435646.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论