简体   繁体   中英

how to post multiple requests using angular.js

I am working on a dynamic form in angular where the data can be multiple paths with other data where on save, it needs to send multiple post requests to server. Is this possible in angular? Here is my html ,

 <fieldset class="form-group pl-sm" ng-repeat="file in files">
   <div>
        <input type="text" class="form-control" autocomplete="off" name="fileLocation" ng-model="file.name">            
    </div>
        <input type="checkbox" class="switch-input" name="" ng-model="file.xxxx" />
        <input type="checkbox" class="switch-input" name="" ng-model="file.yyyy" />            
   <div class="col-sm-3">
        <select class="form-control" name="fileCenters" ng-model="file.centers" required>
            <option value="Development">Development</option>
            <option value="Sales">Sales</option>
        </select>
    </div>
</fieldset>
<a  href="" ng-click="addNew()">Add more </a>

I am not sure how i could save all the paths in a single post request. So essentially it has to be a json object with multiple object entirs such as

 [{
    location: 'france',
    xxxx: true,
    yyyy: false,
    center: 'sales'
   },
   {
    location: 'france',
    xxxx: true,
    yyyy: false,
    center: 'sales'
   }]

any help on angular http.post with multiple request will be great , do i need to queue up the requests with a timeout or add a callback and chain the request on success of the first request??

You do not need to send data multiple times to the server. On submit you can send all the data at once with

$http.post(url, array).then(function(response){
    console.log(response.data);
}).error(function(){
    //If there is an error..
}).finally(function(){
    //regardless of error or not, whatever u want to do
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM