2017-09-25 87 views
-1

我是angularjs的新手。我有這樣一個功能 -For循環使用超時角度js

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); 

} 

在這裏,我想用孤立,但它僅取最後一個元素考慮在內。誰能給我一個解決方案?

回答

1

使用的IIFE可以節省我的情況下每次迭代

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) 

}