Skip to content Skip to sidebar Skip to footer

Passing Parameters To Promise's Callback In Angularjs

I am trying to figure out is there is any way to pass in an index argument to a promise's callback function. For instance. serviceCall.$promise.then(function(object){ $scope.ob

Solution 1:

you can use a closure for that.

for example, in your code, use something like:

functioncallbackCreator(i) {
  returnfunction(executionSteps) {
    $scope.study.cases[i].executionSteps = executionSteps;
  }
}
StudyService.studies.get({id: $routeParams.studyIdentifier})
  .$promise.then(function(study) {
    $scope.study = study;
    for(var i=0;i<study.cases.length;i++) {
      var callback = callbackCreator(i);
      StudyService.executionsteps.get({id: $routeParams.studyIdentifier,caseId:study.cases[i].id})
        .$promise.then(callback);
   }
});

Solution 2:

I've done something similar and I put a promise on each and then used $q.all() on the array of promises like:

$q.all( arrayOfPromises )
.then(function(results) {
   console.log(results[0], results[1], results[2]);
});

Post a Comment for "Passing Parameters To Promise's Callback In Angularjs"