I do the same thing in my projects. Check out the repo here:
https://github.com/ahmadabdul3/mean-flapperNews/tree/master/public/Index/angular
the httpService is my http handler. Then you can look at the postsService, see my getAllPosts function
basically like the comments say, you have to return a promise and handle it in the controller
so you do it like 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 the http service do the 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
}
}
$http.get), but I think the problem is that you need to return the $http promise. Tryfactory.users = function() { return $http(...and we can go from there.response.data, but you can't change a function into an object inside the function itself... (i.e.factory.usersis a function, and so inside this function, trying to assignfactory.usersto a different object isn't going to work).