Javascript 将参数传递给promise';angularjs中的s回调

Javascript 将参数传递给promise';angularjs中的s回调,javascript,angularjs,promise,callback,Javascript,Angularjs,Promise,Callback,我试图弄清楚是否有任何方法可以将索引参数传递给promise的回调函数。比如说 serviceCall.$promise.then(function(object){ $scope.object = object; }); 现在我想传入一个数组索引参数 serviceCall.$promise.then(function(object,i){ $scope.object[i] = something; }); 这能做到吗?请让我知道 下面是代码 StudyService.st

我试图弄清楚是否有任何方法可以将索引参数传递给promise的回调函数。比如说

serviceCall.$promise.then(function(object){
    $scope.object = object;
});
现在我想传入一个数组索引参数

serviceCall.$promise.then(function(object,i){
    $scope.object[i] = something;
});
这能做到吗?请让我知道

下面是代码

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;
      });
  }
});
StudyService.studies.get({id:
$routeParams.studyIdentifier})。$promise.then(函数(研究){
$scope.study=研究;

对于(var i=0;i我做了类似的事情,我对每一个都做了承诺,然后在承诺数组中使用
$q.all()
,比如:

$q.all( arrayOfPromises )
.then(function(results) {
   console.log(results[0], results[1], results[2]);
});
你可以用a来做这个

例如,在您的代码中,使用如下内容:

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);
   }
});
函数回调创建者(i){
返回函数(执行步骤){
$scope.study.cases[i].executionSteps=executionSteps;
}
}
StudyService.studies.get({id:$routeParams.studyIdentifier})
.$PROMITE.then(功能(研究){
$scope.study=研究;

对于(var i=0;IOObject只是另一个名为Study的类,其中包含一个案例列表,每个案例都有一个步骤列表。因此,我需要能够在回调函数内部建立索引。从何处获得
i
?请参阅我刚才在问题中添加的代码。可能的重复详细介绍了定义返回一个可以使用其他数据的回调。这并没有提供将键传递给.then()函数的机会。您只是在JS中硬编码了键