对于使用超时角度js的循环

时间:2017-09-25 14:42:15

标签: javascript angularjs

我是for(var i=0; i < uploadService.getOrphans().length; i++ ) { var orphan = uploadService.getOrphans()[i].attributes.text; $timeout(function () { //Here I am using a orphan but its taking only last element. }, 0); } 的新手。我有这样的功能 -

Dropzone.autoDiscover = false

在这里,我想使用孤儿,但它只考虑最后一个元素。谁能给我一个解决方案?

1 个答案:

答案 0 :(得分:1)

使用IIFE可以为每次迭代保存i的上下文

for(var i=0; i < uploadService.getOrphans().length; i++ ) {
     (function (index) {
       var orphan = uploadService.getOrphans()[index].attributes.text;
       $timeout(function () {
        //Here I am using a orphan but its taking only last element. 
       }, 0);
     })(i)

}