Material angular infinite scroll with $http request

后端 未结 3 1153
轮回少年
轮回少年 2021-01-05 17:20

I\'m using md-virtual-repeat directive of Angular Material to have an infinite scroll, and I need to replace it\'s demo $timeout function with a $h

3条回答
  •  醉酒成梦
    2021-01-05 17:48

    This one works:

    plnkr

    • getItemAtIndex returned the index and not the item
    • if you inspected what you pushed, you'd see that at line 33 in my plunkr I concat obj.data, not plain obj
    (function () {
        'use strict';
        angular.module('infiniteScrolling', ['ngMaterial'])
          .controller('AppCtrl', function ($scope, $http) {
              // In this example, we set up our model using a plain object.
              // Using a class works too. All that matters is that we implement
              // getItemAtIndex and getLength.
              var vm = this;
              vm.infiniteItems = {
                  numLoaded_: 0,
                  toLoad_: 0,
                  items: [],
    
                  // Required.
                  getItemAtIndex: function (index) {
                      if (index > this.numLoaded_) {
                          this.fetchMoreItems_(index);
                          return null;
                      }
                      return this.items[index];
                  },
    
                  // Required.
                  getLength: function () {
                      return this.numLoaded_ + 5;
                  },
    
                  fetchMoreItems_: function (index) {
                      if (this.toLoad_ < index) {
                          this.toLoad_ += 5;
                          $http.get('items.json').then(angular.bind(this, function (obj) {
                              this.items = this.items.concat(obj.data);
                              this.numLoaded_ = this.toLoad_;
                          }));
                      }
                  }
              }
          })
    })();
    

提交回复
热议问题