how to make synchronous http request in angular js

前端 未结 3 1764
北海茫月
北海茫月 2020-12-03 10:27

How to make blocking http request in AngularJS so that i can use the $http response on very next line?

In the following example, $http object doesn\'t

相关标签:
3条回答
  • 2020-12-03 11:02

    Here is a practical answer, courtesy of user Kirill Slatin who posted the answer as a comment. Practical use example at the bottom of the answer.

    If, like me, you need to use that response object as a scope variable, this should work:

    $http.get('URL').success(function(data){
    
    $scope.data = data;
    $.fullCalender = $scope.data;
    $scope.$apply()
    });
    

    $scope.$apply() is what will persist the response object so you can use that data.

    -

    Why would you need to do this?

    I'd been trying to create an "edit" page for my recipes app. I needed to populate my form with the selected recipe's data. After making my GET request, and passing the response data to the $scope.form, I got nothing... $scope.$apply() and Kirill Slatin helped big time. Cheers mate!

    Here's the example from my editRecipeController:

      $http.get('api/recipe/' + currentRecipeId).then(
        function (data) {
          $scope.recipe = data.data;
          $scope.form = $scope.recipe;
          $scope.$apply()
        }
    );
    

    Hope that helps!

    0 讨论(0)
  • 2020-12-03 11:06

    You can use promises for that.

    here is an example:

    $scope.myXhr = function(){
    
        var deferred = $q.defer();
    
        $http({
            url: 'ajax.php',
            method: 'POST',
            data:postData,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            })
            //if request is successful
            .success(function(data,status,headers,config){
    
                //resolve the promise
                deferred.resolve('request successful');
    
            })
            //if request is not successful
            .error(function(data,status,headers,config){
                //reject the promise
                deferred.reject('ERROR');
            });
    
        //return the promise
        return deferred.promise;
    }
    
    $scope.callXhrAsynchronous = function(){
    
        var myPromise = $scope.myXhr();
    
        // wait until the promise return resolve or eject
        //"then" has 2 functions (resolveFunction, rejectFunction)
        myPromise.then(function(resolve){
            alert(resolve);
            }, function(reject){
            alert(reject)      
        });
    
    }
    
    0 讨论(0)
  • 2020-12-03 11:23

    You can't, you'll need deal with it through promises, but you could try do it like this:

    $http.get('URL').success(function(data){
        angular.copy(data, $scope.data);
    });
    
    $.fullCalender({
        data: $scope.data
    });
    

    but most people would just do

    $http.get('URL').success(function(data){
        $.fullCalender({
            data: data
        });
    });
    

    If whatever your fullCalender object is doesn't work with async data, you might need to wrap it in something like ng-if or force it to redraw when the data has been supplied. You can also force the controller to not load until the data is loaded by using the route resolve.

    0 讨论(0)
提交回复
热议问题