我有一个名为'selectedEvents'的事件数组,我需要将它们发送到服务器并逐个获得答案。但是,我不知道如何在成功函数中记住对象。
$scope.moveSelectedEventsToAgency = function(){
var agencyId = $scope.selectedAgency.agencyId;
for (var i = 0; i< $scope.selectedEvents.length; i++){
var event = $scope.selectedEvents[i];
var eventId = event.eventId;
$http({
method: 'PUT',
url: ENV.server_prefix+'/event/ + eventId + "/moveToAgency/" + agencyId'
}).success(function(data, status) {
/* HERE I WANT TO USE EVENT. HOW TO SEND IT HERE FOR DOING ADDITIONAL LOGIC??
*/
}).error(function(data, status, params) {
/* HERE I WANT TO USE EVENT. HOW TO SEND IT HERE FOR DOING ADDITIONAL LOGIC?
*/
});
}
};
将for循环体包含在自执行函数中
$scope.moveSelectedEventsToAgency = function(){
var agencyId = $scope.selectedAgency.agencyId;
for (var i = 0; i< $scope.selectedEvents.length; i++){
// ------------------------------
(function(){
// since its function scope event and eventId will be ok
var event = $scope.selectedEvents[i];
var eventId = event.eventId;
$http({
method: 'PUT',
url: ENV.server_prefix+'/event/ + eventId + "/moveToAgency/" + agencyId'
}).success(function(data, status) {
/* HERE I WANT TO USE EVENT. HOW TO SEND IT HERE FOR DOING ADDITIONAL LOGIC??
*/
}).error(function(data, status, params) {
/* HERE I WANT TO USE EVENT. HOW TO SEND IT HERE FOR DOING ADDITIONAL LOGIC?
*/
});
})();
}
};