use*_*365 16 javascript callback promise angularjs
我想弄清楚是否有任何方法可以将索引参数传递给promise的回调函数.例如.
serviceCall.$promise.then(function(object){
$scope.object = object;
});
Run Code Online (Sandbox Code Playgroud)
现在我想传入一个数组索引参数as
serviceCall.$promise.then(function(object,i){
$scope.object[i] = something;
});
Run Code Online (Sandbox Code Playgroud)
可以这样做吗?请告诉我.
这是下面的代码
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;
});
}
});
Run Code Online (Sandbox Code Playgroud)
Yar*_*mer 21
你可以使用一个闭包.
例如,在您的代码中,使用以下内容:
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);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19718 次 |
| 最近记录: |