2016-10-13 16 views
0

私はpromise$qを使用して非同期呼び出しを行っています。しかし、それは動作しません。

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.jsが正しく読み込まれていません!

+0

あなたはまた、単に返すことができます '$ HTTP({メソッド 'GET'、URL: 'にhttp:// localhost:8080/springhibernateangularjs /サービス/イベント' }) 'それはそれ自体が呼び出し元への約束と委任エラー処理であるからです。 –

答えて

3

これは、あなたが約束ではない結果を返しているので、あなたがデータを取得する方法である:

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

$httpサービスがすでに約束を返すよう$q.deferとの約束を製造する必要はありません。コントローラで

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; 
}); 
関連する問題