faraa faraa - 1 month ago 16
AngularJS Question

$q doesn't work asynchronously

I am using

promise
and
$q
to make an asynchronous call. But it doesn't work.

eventData.js

angular.module('eventsApp').factory('eventData' , function($http ,$q, $log) {
return {

getEvent : function() {

var deferred = $q.defer()
$http({method: 'GET', url: 'http://localhost:8080/springhibernateangularjs/service/events'}).
then(
function(response){
deferred.resolve(response.data);
console.log("succccccc");
},
function(error){

console.log("faiiiiiiil");
deferred.reject(status);
});
return deferred.promise ;
}
};
});


EventContrller.js

$scope.event = eventData.getEvent();


But
$scope.event
is not loading correctly!

Answer

This is how you get the data since you are returning a promise not the results:

eventData.getEvets().then(function(result){
       $scope.event = result;
    })
Comments