传递参数以承诺在cornerjs中的回调

前端之家收集整理的这篇文章主要介绍了传递参数以承诺在cornerjs中的回调前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图找出有没有办法将索引参数传递给承诺的回调函数.例如.
  1. serviceCall.$promise.then(function(object){
  2. $scope.object = object;
  3. });

现在我想传递一个数组索引参数

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

这可以做吗请告诉我.

这是下面的代码

  1. StudyService.studies.get({id:
  2. $routeParams.studyIdentifier}).$promise.then(function(study) {
  3. $scope.study = study;
  4. for(var i=0;i<study.cases.length;i++){
  5. StudyService.executionsteps.get({id:
  6. $routeParams.studyIdentifier,caseId:study.cases[i].id})
  7. .$promise.then(function(executionSteps,i){
  8. $scope.study.cases[i].executionSteps = executionSteps;
  9. });
  10. }
  11. });
你可以使用 closure.

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

  1. function callbackCreator(i) {
  2. return function(executionSteps) {
  3. $scope.study.cases[i].executionSteps = executionSteps;
  4. }
  5. }
  6. StudyService.studies.get({id: $routeParams.studyIdentifier})
  7. .$promise.then(function(study) {
  8. $scope.study = study;
  9. for(var i=0;i<study.cases.length;i++) {
  10. var callback = callbackCreator(i);
  11. StudyService.executionsteps.get({id: $routeParams.studyIdentifier,caseId:study.cases[i].id})
  12. .$promise.then(callback);
  13. }
  14. });

猜你在找的Angularjs相关文章