发送角度发布请求并发送到回调 - 我的对象

时间:2014-06-25 14:07:04

标签: angularjs http put

我有一系列名为' 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 个答案:

答案 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?
                   */
        });

    })();

    }
};