how to make synchronous http request in angular js

前端 未结 3 1782
北海茫月
北海茫月 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: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.

提交回复
热议问题