Tuesday, 15 July 2014

angularjs - how to make synchronous http request in angular js -



angularjs - how to make synchronous http request in angular js -

how create blocking http request in angular js can utilize $http response on next line in angular js.

here $http object doesn't homecoming result on next line can pass $http response on fullcalender javascript library.

here $scope.data returns blank value.

sample code below

$http.get('url').success(function(data){ $scope.data = data; }); $.fullcalender({ data: $scope.data });

you can utilize promises that.

here 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 successful .success(function(data,status,headers,config){ //resolve promise deferred.resolve('request successful'); }) //if request not successful .error(function(data,status,headers,config){ //reject promise deferred.reject('error'); }); //return promise homecoming deferred.promise; } $scope.callxhrasynchronous = function(){ var mypromise = $scope.myxhr(); // wait until promise homecoming resolve or eject //"then" has 2 functions (resolvefunction, rejectfunction) mypromise.then(function(resolve){ alert(resolve); }, function(reject){ alert(reject) }); }

angularjs

No comments:

Post a Comment