AngularJs: $http Synchronous call

前端 未结 1 1754
你的背包
你的背包 2021-01-12 20:08

I have a service to for API call as following,

    getValue: function(input) {
        var deferred, url;
        deferred = $q.defer();
        url = \"url\         


        
相关标签:
1条回答
  • 2021-01-12 20:28

    No that is not possible with Angular.

    See https://github.com/angular/angular.js/blob/master/src/ng/httpBackend.js#L51 where the XMLHttpRequest is opened with

    xhr.open(method, url, true);
    

    The third parameter in an xhr.open() can be set to false or true, where false is synchronous and true is asynchronous. In the Angular case, it is hardcoded to true, so that all outgoing calls will be asynchronous.

    Use the .success() callback to wait until the async call returns, and then do whatever you want to do there.

    As per the suggestion in the comments, you can of course also do the calls via raw javascript, jQuery or any other library that supports synchronous calls, but I would advise using callbacks/defers with the asynchronous angular call, because synchronous calls are blocking and blocking is bad.

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