在angularjs中将参数传递给promise的回调 [英] Passing parameters to promise's callback in angularjs

查看:37
本文介绍了在angularjs中将参数传递给promise的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚是否有任何方法可以将索引参数传递给 Promise 的回调函数.例如.

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.

这是下面的代码

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;
      });
  }
});

推荐答案

您可以使用 关闭.

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

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);
   }
});

这篇关于在angularjs中将参数传递给promise的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆