OnScroll Ionic 2 Event

Choosing when a user scrolls through Ionic 2 is confusing to me. I basically want to say when the 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, because it is left open and there is no other way to close it.

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 undefinedAm I putting it in the wrong place?

+4
source share
2 answers

You can subscribe to content events.

The contents of 3 output events :

  • ionScroll .
  • ionScrollEnd .
  • ionScrollStart , .

:

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

DOM:

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

- :

import { Renderer2 } from '@angular/core';
...
constructor(private renderer: Renderer2) {}

ngOnInit() {
    this.renderer.listen(this.myElement, 'scroll', (event) => {
        // Do something with 'event'
        console.log(this.myElement.scrollTop);
    });
}
0

Source: https://habr.com/ru/post/1673899/


All Articles