topic description
how to write a callback function in angular factory so that controller can get the value in factory
sources of topics and their own ideas
I used $scope.Reddit
because it was loaded asynchronously and couldn"t get the value inside to get the boss"s solution
related codes
var myApp = angular.module("myApp", ["infinite-scroll"]);
myApp.controller("DemoController", function($scope, Reddit) {
$scope.reddit = new Reddit();
//factoryitems
});
// Reddit constructor function to encapsulate HTTP and pagination logic
myApp.factory("Reddit", function($http) {
var Reddit = function() {
this.items = [];
this.busy = false;
this.after = "";
};
Reddit.prototype.nextPage = function() {
if (this.busy) return;
this.busy = true;
var url = "http://api.reddit.com/hot?after=" + this.after + "&jsonp=JSON_CALLBACK";
$http.jsonp(url).success(function(data) {
var items = data.data.children;
for (var i = 0; i < items.length; iPP) {
this.items.push(items[i].data);
}
this.after = "t3_" + this.items[this.items.length - 1].id;
this.busy = false;
}.bind(this));
};
return Reddit;
});
what result do you expect? What is the error message actually seen?
get the data in factory