web services - How to get data from model provider which calls a webservice -
i want retrieve data model provider, getting got 'undefined' in controller.
here code:
controller:
pdmatweb.controller('searchctrl', function($scope, itemmodel){ $scope.updatetablefromsearch = function(){ $scope.mydata = itemmodel.findallitems(); console.log($scope.mydata); };});
provider
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) { }); } } }});
the provider "itemmodel" receives correct data web service. perhaps async problem, i'm not sure.
update
after adding deferred/promise implementation works expected. here final code:
controller:
pdmatweb.controller('searchctrl', function($scope, itemmodel){ $scope.updatetablefromsearch = function(){ itemmodel.findallitems().then(function(data){ console.log(data); $scope.mydata = data; }); }; });
provider
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; } } } });
you dont need deferred accomplish this. $http
returns promise. in first example, reason getting undefined because not returning anything. check findallitems
. not returning anything.
if return $http.get(.....)
should work without using deferred explicitly.
here corrected version :
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: should return data here work. return data; }). error(function (data, status, headers, config) { }); } } }});
Comments
Post a Comment