角度材料虚拟重复与远程数据

时间:2016-07-21 08:07:53

标签: angularjs angular-material

我试图找出angular material Virtual Repeat是如何工作的,文档(对于我可怜的noob编码器)并不十分清楚。 所以我设置了codepen并添加了丢失的代码以连接到RESTfull服务器(在本例中为reddit),但我无法使其正常工作。寻找一个可以解决问题并向我解释一下的好人。

这种方法可能有些不正确之处:

    DynamicItems.prototype.fetchPage_ = function(pageNumber) {
      // Set the page to null so we know it is already being fetched.
      this.loadedPages[pageNumber] = null;

      // For demo purposes, we simulate loading more items with a timed
      // promise. In real code, this function would likely contain an
      // $http request. (OK HERE IS YOUR $HTTP REQUEST)

        this.loadedPages[pageNumber] = [];
        var pageOffset = pageNumber * this.PAGE_SIZE;              
        var url = "https://api.reddit.com/hot?after=" + this.after + "&jsonp=JSON_CALLBACK";        

        $http.jsonp(url).success(function(data) {

          var items = data.data.children;

            for (var i = pageOffset; i < pageOffset + this.PAGE_SIZE; i++) {
               this.loadedPages[pageNumber].push(items[i].data);
          }
          this.after = "t3_" + items[this.loadedPages[pageNumber].length - 1].data.id;   

        }.bind(this));

    };

1 个答案:

答案 0 :(得分:0)

我用来填充数组的迭代是错误的:

for(var i = pageOffset; i < pageOffset + this.PAGE_SIZE; i++){
      this.loadedPages[pageNumber].push(items[i].data);
   }

正确的代码:

for (var i in items){
    this.loadedPages[pageNumber].push(items[i].data);
}