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;
});
}
});
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);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With