欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Angular中使用Intersection Observer API實現(xiàn)無限滾動效果

 更新時間:2023年12月18日 16:38:33   作者:世界還贊頌沉默嗎  
這篇文章主要介紹了Angular中使用Intersection Observer API實現(xiàn)無限滾動,實現(xiàn)原理為 在data下面加一個loading元素 如果此元素進入視窗 則調(diào)用api獲取新的數(shù)據(jù)加到原來的數(shù)據(jù)里面,這時loading就會被新數(shù)據(jù)頂下去,感興趣的朋友一起看看吧

背景:

實現(xiàn)原理為 在data下面加一個loading元素 如果此元素進入視窗 則調(diào)用api獲取新的數(shù)據(jù)加到原來的數(shù)據(jù)里面,這時loading就會被新數(shù)據(jù)頂下去,如此循環(huán)。

<div id="dataContainer"></div>
<div id="loadingContainer"></div>

傳統(tǒng)angular實現(xiàn)是使用ngAfterViewInit()生命周期,寫在指令(Directive)里面,并且傳出一個事件,觸發(fā)時回調(diào)被監(jiān)控組件里面的具體函數(shù)。

不過對于異步操作,元素可能在ngAfterViewInit被調(diào)用時還沒有完成初始化而導(dǎo)致bug,所以用ngAfterViewChecked() 會更穩(wěn),當然也更會導(dǎo)致性能問題,每次變更檢測它都被調(diào)用,這可能會增加應(yīng)用程序的負載。

所以這里使用h5 提供的新API ——交叉觀察器 API(Intersection Observer API)提供了一種異步檢測目標元素與祖先元素或頂級文檔的視口相交情況變化的方法。

效果如圖,為了截到loading,我在增加數(shù)據(jù)的函數(shù)里面加了個等待。

上代碼

watch-to-view.directive.ts

import { Directive, Output,EventEmitter, ElementRef } from '@angular/core';
@Directive({
  selector: '[appWatchToView]'
})
export class WatchToViewDirective {
  private observer:IntersectionObserver;
  @Output() appWatchToView = new EventEmitter<IntersectionObserverEntry>();
  constructor(public el:ElementRef) {
    this.observer = new IntersectionObserver(this.callback,{rootMargin:'100px',threshold:1,root:null});
    this.observer.observe(el.nativeElement);
   }
  public callback = (entries: IntersectionObserverEntry[]) => {
    entries.forEach((entry: IntersectionObserverEntry) => {
      if (entry.isIntersecting) {
        this.appWatchToView.emit(entry);
      }
    })
  }
}

app.component.html

<div id="dataContainer">
    <ul>
        <li class="blueBox" *ngFor="let i of data">{{i}}</li>
    </ul>
</div>
<div id="loadingContainer" class="blueBox" appWatchToView (appWatchToView)="this.loadObserver($event)">Loading more...</div>

app.component.ts

import { Component } from '@angular/core';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'Tour of Herors';
  batch:Number = 10;
  len:Number = this.batch;
  data:Array<Number>=new Array(this.batch).fill(0).map((_,i)=>i);
  loadObserver = async function (event){
    console.log(event.target);
    await new Promise(resolve => setTimeout(resolve, 1000));
    this.data.push(...new Array(this.batch).fill(0).map((_,i)=>i+this.len));
    this.len+=this.batch;
  }
}

再給個樣式吧

  .blueBox{
    display: block;
    width: 100%;
    height: 100px;
    background-color: #38b1ee;
    margin-top: 5px;
    text-align: center;
    color: white;
    font-size: large;
  }

官方link: https://developer.mozilla.org/zh-CN/docs/Web/API/Intersection_Observer_API

到此這篇關(guān)于Angular中使用Intersection Observer API實現(xiàn)無限滾動的文章就介紹到這了,更多相關(guān)Angular無限滾動內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Angular父子組件通過服務(wù)傳參的示例方法

    Angular父子組件通過服務(wù)傳參的示例方法

    這篇文章主要介紹了Angular父子組件通過服務(wù)傳參的示例方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-10-10
  • AngularJS?的生命周期和基礎(chǔ)語法使用詳解

    AngularJS?的生命周期和基礎(chǔ)語法使用詳解

    當你在輸入框中鍵入文本時,這個文本會立即反映到?testString?屬性上,反之亦然,如果你在組件類中改變?testString?的值,輸入框的內(nèi)容也會相應(yīng)更新,這篇文章主要介紹了AngularJS?的生命周期和基礎(chǔ)語法,需要的朋友可以參考下
    2024-05-05
  • AngularJS HTML DOM詳解及示例代碼

    AngularJS HTML DOM詳解及示例代碼

    本文主要介紹AngularJS HTML DOM基礎(chǔ)知識,這里整理了相關(guān)資料和示例代碼進行詳解,有需要的小伙伴可以參考下
    2016-08-08
  • Angular的$http的ajax的請求操作(推薦)

    Angular的$http的ajax的請求操作(推薦)

    這篇文章主要介紹了Angular的$http的ajax的請求操作的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • 詳解Angular2組件之間如何通信

    詳解Angular2組件之間如何通信

    本篇文章主要介紹了詳解Angular2組件之間如何通信,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • angular組件間傳值測試的方法詳解

    angular組件間傳值測試的方法詳解

    這篇文章主要給大家介紹了關(guān)于如何測試angular組件間傳值的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習或者使用angular組件具有一定的參考學(xué)習價值,需要的朋友們下面來一起學(xué)習學(xué)習吧
    2020-05-05
  • 詳解AngularJS1.6版本中ui-router路由中/#!/的解決方法

    詳解AngularJS1.6版本中ui-router路由中/#!/的解決方法

    本篇文章主要介紹了詳解AngularJS1.6版本中ui-router路由中/#!/的解決方法,非常具有實用價值,需要的朋友可以參考下
    2017-05-05
  • AngularJS模態(tài)框模板ngDialog的使用詳解

    AngularJS模態(tài)框模板ngDialog的使用詳解

    這篇文章主要介紹了AngularJS模態(tài)框模板ngDialog的使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • angular2組件中定時刷新并清除定時器的實例講解

    angular2組件中定時刷新并清除定時器的實例講解

    今天小編就為大家分享一篇angular2組件中定時刷新并清除定時器的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 使用Angular 6創(chuàng)建各種動畫效果的方法

    使用Angular 6創(chuàng)建各種動畫效果的方法

    Angular能夠讓我們創(chuàng)建出具有原生表現(xiàn)效果的動畫。我們將通過本文學(xué)習到如何使用Angular 6來創(chuàng)建各種動畫效果。在此,我們將使用Visual Studio Code來進行示例演示。感興趣的朋友跟隨小編一起看看吧
    2018-10-10

最新評論