I have the following AngularJS factory where I consume my API data:
.factory('posts', function posts($http) {
var posts = [];
$http.get('/api').success(function(data) {
posts = data;
});
/* === return this after $http is done === */
return {
get: function(offset, limit) {
return posts.slice(offset, offset + limit);
},
total: function() {
return posts.length;
}
}
});
My problem is that I need to return the get() just after the posts array is populated, when the $http request is done.
Can someone explain me how to achive this?
Return the factory methods just when the $http.get() is done.