Angular之下拉加载功能实现,

import { Component, OnInit, HostListener } from '@angular/core';
import { MethodsService } from "src/app/services/methods.service";
import { Router } from '@angular/router';
import { ActivatedRoute } from '@angular/router';
import { DropDowmService } from '../../../../services/drop-dowm.service';
import { ToastService } from 'src/app/toast.service';
/**
 *@state 食堂发布 档口下菜品详情页面
 */
@Component({
  selector: 'app-cuisine-details',
  templateUrl: './cuisine-details.component.html',
  styleUrls: ['./cuisine-details.component.scss']
})

export class CuisineDetailsComponent implements OnInit {

  //定义变量
  hallId = '';
  diningHall = [];
  isLoadingEnd = false;
  page = 0;

  constructor(
    public apiMethod: MethodsService,
    protected router: Router,
    protected activatedRoute: ActivatedRoute,
    public dropDowmService: DropDowmService,//封装的下拉加载服务
    public toast: ToastService,//弹框
  ) { }

  ngOnInit() {
    //接收页面跳转传递过来的值
    this.activatedRoute.queryParams.subscribe(res => {
      this.hallId = res['hallId'];
    });
    this.queryDiningHall()
  }

  //根据传过来的档口id展示所有菜品信息
  queryDiningHall() {
    this.apiMethod.get('/api/stalls/products?id=' + this.hallId + '&page=' + this.page+ '&size=' + 4, { observe: 'response' }).subscribe(res => {
      for (let i = 0; i < res['body']['length']; i++) {
        this.diningHall.push(res['body'][i]);
      }
      //当api头中的数据次数等价于 循环次数  停止调取api 
      if (Number(res['headers'].get('X-Total-Count')) === this.diningHall.length) {
        this.isLoadingEnd = true;
      }
    }, error => {
      if (error.error.status === 401) {
        this.router.navigate(['./wx-code', { replaceUrl: true }]);
      }
    })
  }


  //滑动加载[下拉翻页]
  @HostListener('window:scroll', ['$event'])
  onWindowScroll(event) {
    const isEnd = this.dropDowmService.calculation();
    if (isEnd && !this.isLoadingEnd) {
      this.toast.load('加载中', 300);
      this.page = this.page + 1;
      //再次查询数据
      this.queryDiningHall();
    }
  }



}

 说明:以上需注意的就是避免陷入死循环,

重复调取api,/* if (Number(res['headers'].get('X-Total-Count')) === this.diningHall.length) { */  判断获取请求头中数据数,与api循环次数等价,即不再调取,显示提示语句:--没有更多内容--

 

 

import {HostListener, Injectable} from '@angular/core';
import {ToastService} from '../toast.service';

@Injectable({
  providedIn: 'root'
})
export class DropDowmService {

  constructor() { }

  //1. 获取滚动条当前位置
  getScrollTop(): number {
    let scrollTop = 0;
    if (document.documentElement && document.documentElement.scrollTop) {
      scrollTop = document.documentElement.scrollTop;
    } else if (document.body) {
      scrollTop = document.body.scrollTop;
    }
    return scrollTop;
  }

  //2. 获取当前可视范围高度
  getClientHeight(): number {
    let clientHeight = 0;
    if (document.body.clientHeight && document.documentElement.clientHeight) {
      clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);
    } else {
      clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);
    }
    return  clientHeight;
  }

// 获取文档完整的高度 = 1+2
  getScrollHeight(): number {
    return  Math.max(document.body.scrollHeight, document.documentElement.clientHeight);
  }

  calculation () {
    console.log(this.getScrollTop())
    console.log(this.getClientHeight())
    console.log(this.getScrollHeight())
    if (this.getScrollTop() + this.getClientHeight()
      >= this.getScrollHeight() ) {
      // console.log('触发');
      return true;
    } else {
      // console.log('没触发');
      return false;
    }
  }

}

本类就是将下拉加载核心代码进行封装成服务,

 

<div *ngFor="let dinings of diningHall;">
    <app-title title="档口名:{{dinings.name}}" class="top-sty"></app-title>
  <div class="show-sty">
    <!--*ngIf="inx<3"显示三条-->
    <div *ngFor="let product of dinings.products;let inx=index" class="modul-sty" >
      <img class="img" src="{{product.imgUrl}}" height="80px" />
      <div style="margin-left: 10px;">{{product.name}}</div>
    </div>
    <div *ngIf="isLoadingEnd" class="footer">
      ————没有更多内容了————
    </div>
  </div>
</div>

这就是html页面,以上即是相关下拉加载的全部代码,

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值