2016-10-13 4 views
0

promise$q을 사용하여 비동기 호출을하고 있습니다. 그러나 그것은 효과가 없습니다.

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(); 

그러나 $scope.event가 제대로로드되지 않는다!

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

또한 단지 반환 할 수 있습니다'$ HTTP ({방법 : 'GET', URL : '통해 http : // localhost : 8080/springhibernateangularjs/서비스/이벤트' })'그 자체가 호출자에게 에러 핸들링을 위임하고 위임하기 때문입니다. –

답변

3

이 당신이 약속이 아닌 결과를 반환하기 때문에 사용자가 데이터를 얻을 방법이다. 컨트롤러에서

app.factory('eventData' , function($http) { 
    return { 

     getEvent : function() { 
      //RETURN http promise 
      return $http.get('http://localhost:8080/springhibernateangularjs/service/events'). 
       then(function(response){ 
        console.log("succccccc"); 
        //return to chain data 
        return response.data; 
       }, 
       function(error){  
        console.log("faiiiiiiil"); 
        //throw to chain rejection 
        throw error; 
       }); 
     } 
    }; 
}); 

:

eventData.getEvent().then(function(data){ 
    $scope.event = data; 
}); 
3

$http 서비스가 이미 약속을 반환로 $q.defer와 약속을 제조 할 필요가 없습니다 :

관련 문제