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

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

问题描述

我想搞清楚的是有没有办法在索引参数承诺的回调函数来传递。例如。

  serviceCall。$ promise.then(函数(对象){
    $ scope.object =对象;
});

现在我想在数组索引参数来传递的。

  serviceCall。$ promise.then(函数(对象,我){
    $ scope.object [I] =东西;
});

可以这样做?请让我知道。

下面是低于

在code

  StudyService.studies.get({ID:
$ routeParams.studyIdentifier})$ promise.then(功能(研究){
$ scope.study =研究;
对于(VAR I = 0; I< study.cases.length;我++){
  StudyService.executionsteps.get({ID:
  $ routeParams.studyIdentifier,caseId:study.cases [I] .ID})
      。$ promise.then(功能(executionSteps,I){
      $ scope.study.cases [I] .executionSteps = executionSteps;
      });
  }
});


解决方案

您可以使用的对于封闭

例如,在code,使用这样的:

 函数callbackCreator(我){
  返回功能(executionSteps){
    $ scope.study.cases [I] .executionSteps = executionSteps;
  }
}
StudyService.studies.get({ID:$ routeParams.studyIdentifier})
  。$ promise.then(功能(研究){
    $ scope.study =研究;
    对于(VAR I = 0; I< study.cases.length;我++){
      变种回调= callbackCreator(ⅰ);
      StudyService.executionsteps.get({ID:$ routeParams.studyIdentifier,caseId:study.cases [I] .ID})
        。$ promise.then(回调);
   }
});

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

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

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