How to write callback function in angular factory so that controller can get the value in factory?

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

Sep.16,2021

var myApp = angular.module('myApp', ['infinite-scroll']);

myApp.controller('DemoController', function($scope, Reddit) {
var afterParam = "xxxxx",
self = this;
    this.busy = true;
   Reddit.nextPage(afterParam).then(
    function(afterid){
        //success
        self.busy = false;
        console.log(afterid) //result from factory
    },function(){
        //error
        self.busy = false;
        //error handler
    })
});

// Reddit constructor function to encapsulate HTTP and pagination logic
myApp.factory('Reddit', function($http) {
  var Reddit = {};
  Reddit.nextPage = function(after) {


    var url = "http://api.reddit.com/hot?after=" + after + "&jsonp=JSON_CALLBACK";
    return $http.jsonp(url).success(function(data) {
      var items = data.data.children;
      
      return "t3_" + items[items.length - 1].data.id;
    });
  };

  return Reddit;
});


Service$sce 

url$sce.trustAsResourceUrl(url)

nextPage 

return $http.jsonp($sce.trustAsResourceUrl(url)).then(function(data) {
        let items = data.data.data.children;
        for (let 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;
        return items;
    }.bind(this));
    



let reddit = new Reddit();
reddit.nextPage().then(function(items) {
    console.log('items:::', items);
});

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e49784-666d8.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e49784-666d8.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?