Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
9
rated 0 times [  14] [ 5]  / answers: 1 / hits: 20474  / 10 Years ago, fri, july 25, 2014, 12:00:00

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.object = object;
});


Now I want to pass in an array index parameter as



serviceCall.$promise.then(function(object,i){
$scope.object[i] = something;
});


Can this be done? Please let me know.



Here is the code below



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

More From » angularjs

 Answers
30

you can use a closure for that.



for example, in your code, use something like:



function callbackCreator(i) {
return function(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);
}
});

[#70036] Thursday, July 24, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
danyelletyanah

Total Points: 204
Total Questions: 109
Total Answers: 108

Location: Vanuatu
Member since Fri, Oct 22, 2021
3 Years ago
;