OnScroll事件Ionic 2

BA1*_*995 10 typescript ionic-framework ionic2 angular

当用户在Ionic 2上滚动时,提起我很困惑.我基本上想说,当用户向下滚动页面时,做一些事情.

任何例子都会很棒.

更新:

我在我的构造函数中有这个,所以当页面滚动时我想关闭键盘,因为它被打开而没有别的方法可以关闭.

import { Component, ViewChild } from '@angular/core';
import { NavController, NavParams, Content } from 'ionic-angular';
import { Keyboard } from '@ionic-native/keyboard';

export class SearchPage {

  @ViewChild(Content)
  content:Content;

  constructor(public keyboard: Keyboard, public formBuilder: FormBuilder, public navCtrl: NavController, public navParams: NavParams, public apiAuthentication: ApiAuthentication, private http: Http) {

    this.content.ionScroll.subscribe((data)=>{
      this.keyboard.close();
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

但是我Cannot read property 'ionScroll' of undefined把它放在错误的地方我得到这个错误?

Ere*_*ald 37

您可以订阅内容事件.

内容有3个输出事件:

  • ionScroll 在每个滚动事件上发出.
  • ionScrollEnd 滚动结束时发出.
  • ionScrollStart 首次滚动时发出.

听一个事件:

@ViewChild(Content)
content: Content;
// ...
ngAfterViewInit() {
  this.content.ionScrollEnd.subscribe((data)=>{
    //... do things
  });
}
Run Code Online (Sandbox Code Playgroud)

或者从DOM做到:

<ion-content (ionScroll)="onScroll($event)">
Run Code Online (Sandbox Code Playgroud)

对于Ionic 4

<ion-content [scrollEvents]="true" (ionScroll)="onScroll($event)">
Run Code Online (Sandbox Code Playgroud)

  • 不适用于ios,你有其他想法吗?或解决方案? (2认同)