-1

I am new to angularjs . I have a function like this -

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

}

Here, I want to use the orphan but it's taking only last element into consideration. Can anyone give me a solution?

A R
  • 1,412
  • 2
  • 14
  • 29
ganesh kaspate
  • 1
  • 9
  • 41
  • 88

1 Answers1

1

Using an IIFE you can save the context of i for each iteration

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)

}
AngelSalazar
  • 3,080
  • 1
  • 16
  • 22