简体   繁体   中英

How to reload a http.get request after performing a function

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.

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();
      });
 }

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) { 

    });
  });
}

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.

});}
});

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
                    });
            };

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