我试图找出有没有办法将索引参数传递给承诺的回调函数.例如.
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; }); } });
你可以使用
closure.
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); } });