Angularjs 如何从md虚拟重复容器中删除空行?

Angularjs 如何从md虚拟重复容器中删除空行?,angularjs,angular-material,Angularjs,Angular Material,下面的代码可以帮助我获取API调用的数据,但我这里有一个问题。每次它在DOM中创建元素并填充我们从API获得的数据时。但如果我得到较少的记录,它将显示空白/空记录。如何删除或停止生成这些空白记录 this.infiniteItems = { numLoaded_: 0, toLoad_: 0, items: [], getItemAtIndex: function (index) { if (index > this.numLoaded_) {

下面的代码可以帮助我获取API调用的数据,但我这里有一个问题。每次它在DOM中创建元素并填充我们从API获得的数据时。但如果我得到较少的记录,它将显示空白/空记录。如何删除或停止生成这些空白记录

this.infiniteItems = {
    numLoaded_: 0,
    toLoad_: 0,
    items: [],
    getItemAtIndex: function (index) {
    if (index > this.numLoaded_) {
        this.fetchMoreItems_(index);
            return null;
        }
        return this.items[index];
     },
     getLength: function() {
        return this.numLoaded_ + 25;
     },
     fetchMoreItems_: function (index) { 
         if (this.toLoad_ < index) {
             this.toLoad_ += 5;
             var offset = 0;

             $http({
                 method: 'GET',
                 datatype: 'json',
                 url: '{my-api-call}',
                 contentType: "application/json; charset=utf-8",
                 cache: false,
                 params: {
                     param: query,
                     page: offset
                 }
              }).then(angular.bind(this, function (obj) {
                 this.items = this.items.concat(obj.data.SearchResults);
                 this.numLoaded_ = this.toLoad_;
                 this.offset++;
                 $scope.searchResults = obj.data.SearchResults;
              }));
         } 
     }
};
this.infiniteItems={
加载数量:0,
toLoad_u0:,
项目:[],
getItemAtIndex:函数(索引){
如果(索引>此.numloated_u41;{
此.fetchMoreItems(索引);
返回null;
}
返回此.items[索引];
},
getLength:function(){
返回此.numloated u+25;
},
fetchMoreItems\函数(索引){
if(this.toLoad<索引){
此.toLoad+=5;
var偏移=0;
$http({
方法:“GET”,
数据类型:“json”,
url:“{my api call}”,
contentType:“应用程序/json;字符集=utf-8”,
cache:false,
参数:{
参数:查询,
页码:偏移量
}
}).then(angular.bind)(这个,函数(obj){
this.items=this.items.concat(obj.data.SearchResults);
this.numLoaded u=this.toloaded;
这个.offset++;
$scope.searchResults=obj.data.searchResults;
}));
} 
}
};

提前谢谢。

我遇到了同样的问题,我这样解决了它:

       $scope.infiniteBrands = {
            numLoaded_: 0,
            toLoad_: 0,
            items: [],
            page: -1,
            hasMoreItems: true,
            perPage: 7,
            //REQUIRED.
            getItemAtIndex: function(index) {
              if (index > this.numLoaded_) {
                this.fetchMoreItems_(index);
                return null;
              }
              return this.items[index];
            },
            //REQUIRED
            getLength: function() {
              return this.numLoaded_ + 1;
            },
            fetchMoreItems_: function(index) {
              if ( this.hasMoreItems && (this.toLoad_ < index)) {
                this.page++;
                this.toLoad_ += this.perPage;
                brandsFactory.getBrands(this.perPage, this.page)
                .then(angular.bind(this, function (data) {
                  this.items = this.items.concat(data);
                  if(data.length < this.perPage){
                    this.numLoaded_ = this.toLoad_ - (this.perPage - data.length) -1;
                    this.hasMoreItems = false;
                  }else{
                    this.numLoaded_ = this.toLoad_;                    
                  }

                }))
                .catch(function(errorResponse){
                  console.log('Error in brands controller, status : ', errorResponse.status);
                });
              }
            }
          };
$scope.infiniteBrands={
加载数量:0,
toLoad_u0:,
项目:[],
第页:-1,
hasMoreItems:是的,
每页:7,
//必需的。
getItemAtIndex:函数(索引){
如果(索引>此.numloated_u41;{
此.fetchMoreItems(索引);
返回null;
}
返回此.items[索引];
},
//必需的
getLength:function(){
返回此.numloated u1;
},
fetchMoreItems:函数(索引){
if(this.hasMoreItems&&(this.toLoad<索引)){
这个.page++;
this.toLoad u+=this.perPage;
brandsFactory.getBrands(this.perPage,this.page)
.then(angular.bind)(此,函数(数据){
this.items=this.items.concat(数据);
如果(data.length<此.每页){
this.numload u=this.toLoad u-(this.perPage-data.length)-1;
this.hasMoreItems=false;
}否则{
this.numLoaded u=this.toloaded;
}
}))
.catch(函数(错误响应){
console.log('brands controller中的错误,状态:',errorResponse.status);
});
}
}
};
如您所见,阻止空行的行包括:

                  if(data.length < this.perPage){
                    this.numLoaded_ = this.toLoad_ - (this.perPage - data.length) -1;
                    this.hasMoreItems = false;
                  }else{
                    this.numLoaded_ = this.toLoad_;                    
                  }
if(data.length
其中this.perPage是我希望每页询问的项目数,但如果服务器返回的数字较低,因为没有更多,我需要从this.numLoaded变量中减去这些项目


虽然已经很晚了,但我希望它能帮助那些有同样问题的人

您应该检查API调用是否已到达最后一页

fetchMoreItems_: function (index) {
                if ((this.toLoad_ < index) && !this.lastPage) {
                    this.toLoad_ += this.size;
                    newsActions.getNews(this.page++, this.size).then(angular.bind(this, function (newsData) {
                        this.items = this.items.concat(newsData.content);
                        this.numLoaded_ = this.toLoad_ >= newsData.totalElements ? newsData.totalElements : this.toLoad_  ;
                        this.lastPage = newsData.last;
                    }));
                }
            }

你有工作样品要看吗?在本例中,请从items.json文件中删除一些项,然后您会看到空行。您找到解决方法了吗?我也有同样的问题
getLength: function () {
              if(this.lastPage){
                return this.numLoaded_
              }
              return this.numLoaded_ + 2;
            },