javascript - How to return a http get request from rest api with angular factory? -


i trying return http request rest api url inside factory. it's not returning data instead function. when use console log inside controller show whole function.

myapp.factory('userfactory', function($http) {      var factory = [];      factory.users = function(){         $http({             url: '/test/users',             method: 'get'         })         .then(function successcallback(response) {             factory.users = response;          }, function errorcallback(response) {             factory.users = response;         });          return factory.users;     };      return factory;  }); 

i same thing in projects. check out repo here: https://github.com/ahmadabdul3/mean-flappernews/tree/master/public/index/angular

the httpservice http handler. can @ postsservice, see getallposts function

basically comments say, have return promise , handle in controller

so this:

function getallposts() {         httpservice.baseget(httpurls.posts)         .then(function(data) {             angular.copy(data, posts);             posts.push({title: 'post test', link: '', upvotes: 3, comments: []});         }, function(data) {             httpservice.handleerror(data);         });     } 

and have http service work:

angular.module('httpservice', []) .factory('httpservice', httpservice);  httpservice.$inject = ['$http', '$q'];  function httpservice($http, $q) {     var serv = this;     var methods = {         httpget : 'get',         httppost : 'post',         httpput : 'put',         httpdelete : 'delete'     };     function baseget(url) {         return $http.get(url).then(                 function (result) {                     return result.data;                 },                 function (result) {                     return $q.reject(result);                 }                 );     }     function httpwithparams(url, method, data) {          return $http({                 url: url,                 method: method,                 params: data,                 datatype: "json",                 headers: {                     "content-type": "application/json"                 }             }).then(                 function (result) {                     console.log(result.data);                     return result.data;                 },                 function (result) {                     return $q.reject(result);                 }             );     }     function handleerror(error) {             console.log(error);     }      return {         baseget: baseget,         httpwithparams: httpwithparams,         handleerror: handleerror,         methods: methods     } } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -