2013-03-25 1 views
0

모델 공급자로부터 데이터를 가져오고 싶지만 내 컨트롤러에 '정의되지 않음'이 모두 있습니다.웹 서비스를 호출하는 모델 제공 업체의 데이터를 가져 오는 방법

컨트롤러 :

pdmAtWeb.controller('SearchCtrl', function($scope, ItemModel){ 
$scope.updateTableFromSearch = function(){ 
     $scope.myData = ItemModel.findAllItems(); 
     console.log($scope.myData); 
};}); 

제공자

pdmAtWeb.provider('ItemModel', function() { 
this.defaultEndpoint = '/item'; 
this.defaultServiceUrl = 'http://localhost:8080/webservice'; 

this.setDefaultEndpoint = function (newEndpoint) { 
    this.defaultEndpoint = newEndpoint; 
}; 

this.setDefaultServiceUrl = function (newServiceUrl) { 
    this.defaultServiceUrl = newServiceUrl; 
} 

this.$get = function ($http) { 
    var endpoint = this.endpoint; 
    var serviceUrl = this.serviceUrl; 

    var refreshConnection = function() { 
     // reconnect 
    } 
    return{ 
     findAllItems: function() { 
      $http({method: 'GET', url: serviceUrl + endpoint}). 
       success(function (data, status, headers, config) { 
        console.log(data); 
        return data; 

       }). 
       error(function (data, status, headers, config) { 

       }); 
     } 
    } 
}}); 

제공자 "ItemModel"는 웹 서비스로부터 올바른 데이터를 수신하고 여기서

코드이다. 아마도 이것은 비동기 문제이지만 확실하지 않습니다. 예상대로 작동하는 연기/약속 구현을 추가 한 후

UPDATE

. 여기에 최종 코드는 다음과 같습니다

컨트롤러 :

pdmAtWeb.controller('SearchCtrl', function($scope, ItemModel){ 
$scope.updateTableFromSearch = function(){ 
     ItemModel.findAllItems().then(function(data){ 
      console.log(data); 
      $scope.myData = data; 
     }); 
}; 
}); 

제공

pdmAtWeb.provider('ItemModel', function() { 
    this.defaultEndpoint = '/item'; 
    this.defaultServiceUrl = 'http://localhost:8080/webservice'; 

    this.setDefaultEndpoint = function (newEndpoint) { 
     this.defaultEndpoint = newEndpoint; 
    }; 

    this.setDefaultServiceUrl = function (newServiceUrl) { 
     this.defaultServiceUrl = newServiceUrl; 
    } 

    this.$get = function ($http, $q) { 
     var endpoint = this.defaultEndpoint; 
     var serviceUrl = this.defaultServiceUrl; 

     var refreshConnection = function() { 
      // reconnect 
     } 
     return{ 
      findAllItems: function() { 
       var deferred = $q.defer(); 

       $http({method: 'GET', url: serviceUrl + endpoint}). 
        success(function (data, status, headers, config) { 
        deferred.resolve(data); 
        }). 
        error(function (data, status, headers, config) { 
         deferred.reject(); 
        }); 
       return deferred.promise; 
      } 
     } 
    } 
}); 

답변

0

당신은이를 달성하기 위해 연기 필요 없어요. $http이 이미 약속을 반환합니다. 첫 번째 예에서는 아무 것도 반환하지 않기 때문에 정의되지 않은 이유가 있습니다. findAllItems을 확인하십시오. 아무것도 반환하지 않습니다.

return $http.get(.....)을하면 모든 것이 명시 적으로 지연되지 않고 작동합니다.

pdmAtWeb.provider('ItemModel', function() { 
this.defaultEndpoint = '/item'; 
this.defaultServiceUrl = 'http://localhost:8080/webservice'; 

this.setDefaultEndpoint = function (newEndpoint) { 
    this.defaultEndpoint = newEndpoint; 
}; 

this.setDefaultServiceUrl = function (newServiceUrl) { 
    this.defaultServiceUrl = newServiceUrl; 
} 

this.$get = function ($http) { 
    var endpoint = this.endpoint; 
    var serviceUrl = this.serviceUrl; 

    var refreshConnection = function() { 
     // reconnect 
    } 
    return{ 
     findAllItems: function() { 
      //NOTE addition of return below. 
      return $http({method: 'GET', url: serviceUrl + endpoint}). 
       success(function (data, status, headers, config) { 
        console.log(data); 
        //NOTE: YOU SHOULD ALSO return data here for it to work. 
        return data; 

       }). 
       error(function (data, status, headers, config) { 

       }); 
     } 
    } 
}}); 
: 여기

는 수정 된 버전입니다
관련 문제