How to reload a http.get request after performing

2019-07-15 05:25发布

问题:

I am trying to delete a post from a list. The delete function is performing by passing serially to a delete function showed below.

$scope.go = function(ref) {
  $http.get("api/phone_recev.php?id="+ref)
  .success(function (data) { });
}

After performing the function, I need to reload the http.get request which used for listing the list.

$http.get("api/phone_accept.php")
.then(function (response) { });

Once the function performed. The entire list will reload with new updated list. Is there any way to do this thing.

回答1:

Try this

$scope.go = function(ref) {
  $http.get("api/phone_recev.php?id="+ref)
  .success(function (data) {
  //on success of first function it will call

    $http.get("api/phone_accept.php")
     .then(function (response) { 

    });
  });
}


回答2:

function list_data() {
    $http.get("api/phone_accept.php")
       .then(function (response) { 
            console.log('listing');
         });
 }

 $scope.go = function(ref) {
   $http.get("api/phone_recev.php?id="+ref)
    .success(function (data) { 
            // call function to do listing
           list_data();
      });
 }


回答3:

Like what @sudheesh Singanamalla says by calling the same http.get request again inside function resolved my problem.

$scope.go = function(ref) {
$http.get("api/phone_recev.php?id="+ref).success(function (data) { 

//same function goes here will solve the problem.

});}
});


回答4:

You can use $q - A service that helps you run functions asynchronously, and use their return values (or exceptions) when they are done processing.

https://docs.angularjs.org/api/ng/service/$q

Inside some service.

app.factory('SomeService', function ($http, $q) {
        return {
            getData : function() {
                // the $http API is based on the deferred/promise APIs exposed by the $q service
                // so it returns a promise for us by default
                return $http.get("api/phone_recev.php?id="+ref)
                    .then(function(response) {
                        if (typeof response.data === 'object') {
                            return response.data;
                        } else {
                            // invalid response
                            return $q.reject(response.data);
                        }

                    }, function(response) {
                        // something went wrong
                        return $q.reject(response.data);
                    });
            }
        };
    });

function somewhere in controller

            var makePromiseWithData = function() {
                // This service's function returns a promise, but we'll deal with that shortly
                SomeService.getData()
                    // then() called when gets back
                    .then(function(data) {
                        // promise fulfilled
//                        something
                    }, function(error) {
                        // promise rejected, could log the error with: console.log('error', error);
                        //some code
                    });
            };