Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
766 views
in Technique[技术] by (71.8m points)

angular - OnScroll event Ionic 2

Picking up when a user scroll on Ionic 2 is confusing me. I basically want to say, when a user scrolls down the page, do something.

Any examples would be great.

UPDATE:

I have this in my constructor, so when the page scrolls I want to close the keyboard, due to it being left open and no other way to close.

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();
    });
  }
}

However I get this error Cannot read property 'ionScroll' of undefined am i putting it in the wrong place?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can subscribe to content events.

Content has 3 output events:

  • ionScroll Emitted on every scroll event.
  • ionScrollEnd Emitted when scrolling ends.
  • ionScrollStart Emitted when the scrolling first starts.

Listen to an event:

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

Or do it from the DOM:

<ion-content (ionScroll)="onScroll($event)">

For Ionic 4

<ion-content [scrollEvents]="true" (ionScroll)="onScroll($event)">

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...