Angularjs 在角度工厂内使用角度变量

Angularjs 在角度工厂内使用角度变量,angularjs,nginfinitescroll,Angularjs,Nginfinitescroll,我是新来的。。。在下面的代码中,我需要在角度工厂(测试)内部使用变量scope$scope.slugname var myApp = angular.module('myApp', ['infinite-scroll']); myApp.controller('DemoController', function($scope, Test) { $scope.slugname ="slugname"; $scope.test = new Test(); }); myApp.factor

我是新来的。。。在下面的代码中,我需要在角度工厂(测试)内部使用变量scope
$scope.slugname

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

myApp.controller('DemoController', function($scope, Test) {
  $scope.slugname ="slugname";
  $scope.test = new Test();
});

myApp.factory('Test', function($http) {
  var test = function() {
    this.items = [];
    this.busy = false;
    this.after = '';
  };

  Test.prototype.nextPage = function() {
    if (this.busy) return;
    this.busy = true;

    var url = 'baseURL/?slug='+$scope.slugname;  //<-- I need to use variable($scope.slugname) here
    $http.jsonp(url).success(function(data) {
      var items = data.data.children;
      for (var i = 0; i < items.length; i++) {
        this.items.push(items[i].data);
      }
      this.after =  this.items[this.items.length - 1].id;
      this.busy = false;
    }.bind(this));
  };

  return Test;
});
var myApp=angular.module('myApp',['infinite-scroll']);
myApp.controller('DemoController',函数($scope,Test){
$scope.slugname=“slugname”;
$scope.test=新测试();
});
myApp.factory('Test',函数($http){
var测试=函数(){
此参数为.items=[];
this.busy=false;
这个。在=“”;
};
Test.prototype.nextPage=函数(){
如果(这个忙)返回;
this.busy=true;

var url='baseURL/?slug='+$scope.slugname;//不能在工厂内使用范围变量。或者,您可以将范围变量作为参数传递给工厂函数

myApp.controller('DemoController', function($scope, Test) {
  $scope.slugname ="slugname";
  $scope.test = new Test($scope.slugname );
});

myApp.factory('Test', function($http) {
  var test = function() {
    this.items = [];
    this.busy = false;
    this.after = '';
  };

  Test.prototype.nextPage = function(name) {
    if (this.busy) return;
    this.busy = true;

    var url = 'baseURL/?slug='+$scope.slugname;
    $http.jsonp(url).success(function(data) {
      var items = data.data.children;
      for (var i = 0; i < items.length; i++) {
        this.items.push(items[i].data);
      }
      this.after =  this.items[this.items.length - 1].id;
      this.busy = false;
    }.bind(this));
  };

  return Test;
});
myApp.controller('DemoController',函数($scope,Test){
$scope.slugname=“slugname”;
$scope.test=新测试($scope.slugname);
});
myApp.factory('Test',函数($http){
var测试=函数(){
此参数为.items=[];
this.busy=false;
这个。在=“”;
};
Test.prototype.nextPage=函数(名称){
如果(这个忙)返回;
this.busy=true;
var url='baseURL/?slug='+$scope.slugname;
$http.jsonp(url).success(函数(数据){
变量项=data.data.children;
对于(变量i=0;i
var-url='baseURL/?slug='+$scope.slugname;
而不是
var-url='baseURL/?slug='+name;
Right...yp。只需替换factorysure中的作用域变量。如果有帮助,请确保将其标记为应答。