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