简体   繁体   English

Angular2 / Ionic2:在侦听器中执行$ http.get

[英]Angular2 / Ionic2 : Execute $http.get within Listener

I am trying to fetch datas on scroll and fill an array of " Article[] ". 我试图获取滚动数据并填充“ Article [] ”数组。

httpLoadArticles() {
    this.httpWSGetInit().subscribe(
                                   articles => this.articles = articles,
                                    err => {
                                        console.log(err);
                                    },
                    () => console.log('Searching complete')
}

And the current function. 和当前功能。

httpWSGetInit() : Observable<Article []> {

    return this.http.get(R.WEBSITE_URL_WS + this.articlesPagination)
                         .map((res:Response) => res.json())
                         .catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}

Works pretty well. 效果很好。

But the http.get method is never called in addScrollListener . http.get方法永远不会在一个名为addScrollListener

@ViewChild(Content) content: Content;

ionViewDidLoad() {
        this.content.addScrollListener(this.onPageScroll);
}

onPageScroll(event) {
       this.httpLoadArticles()
}

I tried to set the GET as synchronous but it seems to not change anything. 我试图将GET设置为synchronous但似乎没有任何改变。 Is it a scope issue ? 这是scope问题吗? Like addScrollListener is a pure JS method and cannot be attached to angular2 container ? 就像addScrollListener是纯JS方法一样,无法附加到angular2容器吗?

Yes its an scope issue. 是的,这是一个范围问题。 You should use Arrow function to preserve this context 您应该使用Arrow函数保留此上下文

ionViewDidLoad() {
    this.content.addScrollListener(() => this.onPageScroll() );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM