Convert factory to service in Angularjs -
i'm using $resource
fetch json server working fine:
topicservices.factory('topic', ['$resource', function($resource){ return $resource('http://localhost/topic_api_response.json', {}, { fetchdata: {method:'get', isarray:true} }); }]);
however, want convert factory service doing this:
topicservices.service('topic', ['$resource', function($resource) { this.fetchdata = function() { var resp = $resource('http://localhost/topic_api_response.json'); return resp; }; }]);
in doing so, i'm not able figure out specify request method (get/post
) , give get/post
params
going "service" style of defining, should read angular docs.
anyways here how go: have separated out fetchdata method, seems hit http get. hence can write code follows.
topicservices.service('topic', ['$http', function($http) { this.fetchdata = function() { var resp = $http.get('http://localhost/topic_api_response.json'); return resp; }; }]);
if want use $resource wrap below:
topicservices.service('topic', ['$resource', function($resource) { this.myresource = function() { var resp = $resource('http://localhost/topic_api_response.json',params,{fetchdata:{}}); return resp; }; }]);
Comments
Post a Comment