serv.factory('loadData', function($http) {
var test = [];
var uri = 'http://jsonplaceholder.typicode.com/posts';
$http.get(uri).then(function(response) {
test = response.data;
});
return {
all : function() {
return test;
}
};
});
But you have to think async … your test var is still empty because $http is probably not ready yet executing.
The solution is to make a promise function in the factory. In the function execute the $http and then return the reject/resolve object back to the controller. If the controller recieves a resolve, fill the scope var with the result of the resolve object.