Sunday 15 March 2015

angularjs - Passing variable to promise in a loop -


I have a promise in a loop, and I do not know how to pass some scope variations in the promise handler. /p>

  (superarray in var i) {MyService.get (superstar [i] .externalID) .then (function (r) {console.debug (i);});   

MyService is a working service, with a method that promises.

  app.factory ('MyService', function ($ http, $ Q) {return: task (item id) {var deferred = $ q.defer (); $ http.get ( '/ Someresturl /' + itemID). Then (function (e) {deferred.resolve (e.data);}, function (reason) {deferred.reject (cause);}); Returns; Promote;}} );   

In my console, console.debug does not display logicaly 1,2,3,4,5 but 5,5,5,5,5 (there are 5 elements in my superhero) .

How can I pass the value of 'i' in the realm of my promise, so can I use it in ()?

Is this possible?

There is a way to capture i in a closing:

  (var i in superarray) {(function (i) {MyService.get (superarray [i] .externalID) .then (function (r) {console.debug (i);} );}) (I); }   

Another way to repeat as the property of r will be to arrange for itemID : < Pre> for (var i in Super IR) {MyService.get (Supraatara [i] .externalID). Then (function (r) {console.debug (r.itemID);}); };

No comments:

Post a Comment