2017-04-03 96 views
2

當用戶在Ionic 2上滾動時拾取使我感到困惑。我基本上想說,當用戶向下滾動頁面時,做一些事情。OnScroll事件Ionic 2

任何例子都會很棒。

UPDATE:

我有這個在我的構造函數,所以當頁面滾動我要關閉鍵盤,因爲它保持打開,並沒有其他的方式來關閉。

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

但是我得到這個錯誤Cannot read property 'ionScroll' of undefined我把它放在錯誤的地方?

+0

我假設你在視圖中有一個''標籤?你可能不得不在離子視圖生命週期中的事件中訂閱事件,而不是構造器。 – devqon

回答

17

您可以訂閱內容事件。

內容有3 output events

  • ionScroll每滾動事件觸發。
  • ionScrollEnd滾動結束時發出。
  • ionScrollStart滾動首次啓動時發出。

聽的事件:

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

還是從DOM做到這一點:

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

謝謝!我更新了一個錯誤,我得到了問題 – BA1995

+0

你有沒有得到子組件'Content'? – Erevald

+0

是的,我再次更新以顯示.ts文件的主要方面 – BA1995

0

當自定義指令內嘗試是這樣的:

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