【问题标题】:Angular2 Infinite Scroll implementationAngular2无限滚动实现
【发布时间】:2017-05-02 11:19:12
【问题描述】:

我正在为我的项目使用 angular2-infinite-scroll。我的想法是第一次页面将加载 6 个项目,然后每次滚动到页面末尾时,它应该再渲染 6 个项目。

但是当我滚动时,它会持续加载并且永不停止,尽管我的 JSON 文件中只有 15 个项目。

这是我的ReviewComponent.ts

import { Component, OnInit } from '@angular/core';
import { AuthService } from '../../services/auth.service';
import { ApiService } from '../../services/api.service';
import { Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Review } from '../../model/review.model';
import { InfiniteScroll } from 'angular2-infinite-scroll';

@Component({
  selector: 'app-home',
  templateUrl: './review.component.html',
  styleUrls: ['./review.component.css']
})
export class ReviewComponent implements OnInit {

  title = 'Hello InfiniteScroll v0.2.8, Ng2 Final';

  datas: any[];
  array = [];
  sum = 40;
  throttle = 300;
  scrollDistance = 1;
  errorMessage: string;

  constructor(private _auth: AuthService,
              private _api: ApiService) { 
               this.addItem(0, this.sum)
              }

  ngOnInit() {
      this.getReviewList();
  }
 getReviewList() {
   this._api.getApi("http://localhost:4200/assets/smock/api/reviewList.json")
             .subscribe(data => this.datas = data,
                         error => this.errorMessage = <any>error) 

 }
addItem(startIndex, endIndex) {
    for (let i = 0; i < this.sum; ++i) {
      this.array.push(i);
    }
  }

onScrollDown() {
    console.log('scrolled!!');
    // add another 6 items
    const start = this.sum;
    this.sum += 6;
    this.addItem(start, this.sum);
  }

}

我的 ReviewComponent.htm:

<div class="page-name">
            <h1><i class="large material-icons">create</i></h1>
            <h1>Thảo luận</h1>
        </div>
        <div class="search-form">
            <input type="text" placeholder="Tìm kiếm...">
            <a href="#!"><i class="material-icons">search</i></a>
        </div>

  <div class="search-results"
         infinite-scroll
         [infiniteScrollDistance]="scrollDistance"
         [infiniteScrollThrottle]="throttle"
         (scrolled)="onScrollDown()">

    <div class="card-review" *ngFor="let i of array">
           <p>{{i}}</p>
        </div>         
</div>       

这是我的仓库:https://github.com/linhho/X-project_frontend/tree/master/XFront

【问题讨论】:

    标签: javascript angular infinite-scroll


    【解决方案1】:

    ReviewComponent.ts里面onScrollDown()方法比较输出数组的长度和滚动限制

    import { Component, OnInit } from '@angular/core';
    import { AuthService } from '../../services/auth.service';
    import { ApiService } from '../../services/api.service';
    import { Response } from '@angular/http';
    import { Observable } from 'rxjs/Observable';
    import { Review } from '../../model/review.model';
    import { InfiniteScroll } from 'angular2-infinite-scroll';
    
    @Component({
      selector: 'app-home',
      templateUrl: './review.component.html',
      styleUrls: ['./review.component.css']
    })
    export class ReviewComponent implements OnInit {
    
      title = 'Hello InfiniteScroll v0.2.8, Ng2 Final';
    
      datas: any[];
      array = [];
      sum = 40;
      throttle = 300;
      scrollDistance = 1;
      errorMessage: string;
    
      constructor(private _auth: AuthService,
                  private _api: ApiService) { 
                   this.addItem(0, this.sum)
                  }
    
      ngOnInit() {
          this.getReviewList();
      }
    
     getReviewList() {
       this._api.getApi("http://localhost:4200/assets/smock/api/reviewList.json")
                 .subscribe(data => this.datas = data,
                             error => this.errorMessage = <any>error) 
    
     } 
    
    addItem(startIndex, endIndex) {
        for (let i = 0; i < this.sum; ++i) {
          this.array.push(this.datas[i]);
        }
      }
    
    onScrollDown() {
        console.log('scrolled!!');
        // add another 6 items
        const start = this.sum;
        this.sum += 6;
         if(this.sum<this.datas.length){
            this.addItem(start, this.sum);
         }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      相关资源
      最近更新 更多