javascript – 带$http请求的材质角度无限滚动

我正在使用Angular Material的md-virtual-repeat指令进行无限滚动,我需要用$http请求替换它的 demo $timeout函数.但我无法找到正确的解决方案.
在下面的代码中,无限滚动工作正常,但不显示来自http请求的数据.问题是我不知道将$http结果绑定到infiniteItems的方法.

Here是plunker.

的index.html

<body ng-app="infiniteScrolling" class="virtualRepeatdemoInfiniteScroll">
<div ng-controller="AppCtrl as ctrl" ng-cloak>
    <md-content layout="column">
        <md-virtual-repeat-container id="vertical-container" flex>
            <div md-virtual-repeat="item in ctrl.infiniteItems" md-on-demand
                 class="repeated-item" flex>
                {{item.id}}
            </div>
        </md-virtual-repeat-container>
    </md-content>
</div>
</body>

JS:

(function () {
'use strict';
angular
  .module('infiniteScrolling',['ngMaterial'])
  .controller('AppCtrl',function ($timeout,$scope,$http) {
     this.infiniteItems = {
          numloaded_: 0,toLoad_: 0,items:[],getItemAtIndex: function (index) {
              if (index > this.numloaded_) {
                  this.fetchMoreItems_(index);
                  return null;
              }
              return index;
          },getLength: function () {
              return this.numloaded_ + 5;
          },fetchMoreItems_: function (index) {
               if (this.toLoad_ < index) {
                  this.toLoad_ += 20;

                  $http.get('items.json').success(function (data) {
                      var items = data;
                      for (var i = 0; i < items.length; i++) {
                          this.items.push(items[i].data);
                      }
                      this.numloaded_ = this.toLoad_;
                  }.bind(this));
              }
          }
      };
   });
})();

解决方法

这个工作:

plnkr

> getItemAtIndex返回索引而不是项目
>如果你检查过你所推动的东西,你会看到我的插件中的第33行,我的结论是obj.data,而不是简单的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,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_;
                      }));
                  }
              }
          }
      })
})();

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...