ionic3 主动触发下拉刷新指令
用法
页面加载的时候触发
每次进入页面视图的时候触发
指令代码
import { Directive, OnInit, Injector, Input } from '@angular/core';
import { Refresher, Content, ViewController } from 'ionic-angular';
@Directive({
selector: '[beginRefresh]'
})
export class BeginRefreshDirective implements OnInit {
@Input('beginRefresh') private _beginRefresh = false;
constructor(
private injector: Injector,
private viewCtrl: ViewController
) {
}
ngOnInit() {
if (!this._beginRefresh) {
this.beginRefresh();
} else {
this.viewCtrl.didEnter.subscribe(() => {
this.beginRefresh();
});
}
}
private async beginRefresh() {
const top = await this.getContentTop();
const refresher = this.injector.get(Refresher);
refresher._top = `${top}px`;
refresher.state = 'refreshing';
refresher._beginRefresh();
}
private getContentTop(): Promise
const content = this.injector.get(Content);
return new Promise((resolve, reject) => {
const timer = setInterval(() => {
if (content.contentTop) {
resolve(content.contentTop);
clearInterval(timer);
}
}, 0);
});
}
}