#angularjs #http #put
#angularjs #http #поместить
Вопрос:
У меня есть массив событий, называемых ‘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?
*/
});
}
};
Ответ №1:
просто вложите тело цикла 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?
*/
});
})();
}
};