我有一系列名为' 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?
*/
});
}
};
答案 0 :(得分:3)
将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?
*/
});
})();
}
};