角度指令中的无限滚动

时间:2015-04-20 06:55:59

标签: javascript angularjs angularjs-directive infinite-scroll

我为无限滚动编写了以下指令,我的问题是我无法弄清楚为什么它只是在加载指令时触发一次,我需要你的建议如何使我的列表无限滚动。

我使用它来远程获取数据,每次我调用它时,我都会添加到计数器25,所以每次都会返回更多数据。

感谢名单,



angular.module('MyApp')
  .controller('InboxCtrl', function($scope, InboxFactory) {

    var counter = 0;

    $scope.loadData = function() {
      var promise = InboxFactory.getEvents(counter);
      promise.then(function(result) {
        $scope.events = result;
      });
      counter += 25;
    };

  });

angular.module('MyApp')
  .factory('InboxFactory', function($http, $q) {
    // Service logic

    var defered = $q.defer();

    function getUrl(count) {
      return "api/inbox/get?request={'what':'Search','criteria':'inbox','criteriaId':null,'startTime':null,'endTime':null,'offset':" + count + ",'limit':25,'order':'event_time','direction':'DESC','source':''}";
    }

    function extract(result) {
      return result.data.data;
    }

    // Public API here
    return {
      getEvents: function(count) {
        $http.get(getUrl(count)).then(
          function(result) {
            defered.resolve(extract(result))
          }, function(err) {
            defered.reject(err);
          }
        );

        return defered.promise;
      }
    };
  });

angular.module('MyApp')
  .directive('infiniteScroll', ['$timeout',
    function(timeout) {
      return {
        link: function(scope, element, attr) {
          var
            lengthThreshold = attr.scrollThreshold || 50,
            timeThreshold = attr.timeThreshold || 400,
            handler = scope.$eval(attr.infiniteScroll),
            promise = null,
            lastRemaining = 9999;

          lengthThreshold = parseInt(lengthThreshold, 10);
          timeThreshold = parseInt(timeThreshold, 10);

          if (!handler || !components.isFunction(handler)) {
            handler = components.noop;
          }

          element.bind('scroll', function() {
            var
              remaining = element[0].scrollHeight - (element[0].clientHeight + element[0].scrollTop);

            //if we have reached the threshold and we scroll down
            if (remaining < lengthThreshold && (remaining - lastRemaining) < 0) {

              //if there is already a timer running which has no expired yet we have to cancel it and restart the timer
              if (promise !== null) {
                timeout.cancel(promise);
              }
              promise = timeout(function() {
                handler();
                promise = null;
              }, timeThreshold);
            }
            lastRemaining = remaining;
          });
        }

      };
    }
  ]);
&#13;
<ul class="inbox-list" infinite-scroll="loadData()">
  <li class="clearfix" ng-repeat="event in events">{{event}}</li>
</ul>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

我做了一些更改,更重要的是使用ng-transclude和为指令传递方法和参数创建新范围。您可以查看jsbind。当然数据是硬编码的,所以我可以伪造行为。

<ul class="inbox-list" my-infinite-scroll composite-method="loadData()">