简体   繁体   中英

Angular $http call to synchronous

angular.module('sample').factory('Service',
function( $rootScope, $http, $location )
{
    return {
        loadInfo: function()
        {
            var _url = 'url';
            $http.get( _url )
                .success( function( data )
                {


                    console.log(data);
                })
                .error( function( response )
                {
                    error( response );
                });
        },
});

I cannot call any other services without getting this result , but as it is asynchronous in weak networks it is getting time to load this results and end up with errors . How can i make this call to synchronous. Idea is other all services should wait until it loaded. In jquery ajax we can set a true or false very easily.But i am not able to do it in angular ,I refereed some other answers also but nothing seems to working .

Please suggest

You can make use of angular promises. Documentation on $q service

angular.module('sample').factory('Service',
['$rootScope','$http','$location','$q',function( $rootScope, $http, $location,$q )
{
    return {
        loadInfo: function()
        {
            var _url = 'url';
            var deferred = $q.defer();
            $http.get( _url )
                .success( function( data )
                {

                    deferred.resolve(data);
                    console.log(data);
                })
                .error( function( response )
                {
                    deferred.reject(data);
                    error( response );
                });
        },
});

In your Controller it will look like this then:

Service.loadInfo.then(
     function(data){
         //Whatever you want to do with the data  on success     
     }
    ,function(data){
         //Whatever you want to do on error
     }
 );

Edit ,

I have added $q service and some resolves and defers. Please check the updated answer.

It would have been really helpful if you could provide us with more code

You made a very simple error in your code

angular.module('sample').factory('Service',
function( $rootScope, $http, $location,$q )
{
    return {
        loadInfo: function()
        {
            var _url = 'url';
            var deferred = $q.defer();
            return $http.get( _url )
                .success( function( data )
                {

                    console.log(data);
                    deferred.resolve(data);
                })
                .error( function( response )
                {
                    error( response );
                    deferred.reject(response);
                });
        },
});

Edit In order to access 'then' , you need to return a promise, which I created using $q.defer() and returning deferred.resolve().

Cannot read property 'then' of undefined 

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