【问题标题】:Angular2 / Ionic2 : Execute $http.get within ListenerAngular2 / Ionic2:在监听器中执行 $http.get
【发布时间】:2016-11-21 21:23:49
【问题描述】:

我正在尝试获取滚动数据并填充“Article[]”数组。

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

还有当前函数。

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

效果很好。

http.get 方法从未在addScrollListener 中调用。

@ViewChild(Content) content: Content;

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

onPageScroll(event) {
       this.httpLoadArticles()
}

我尝试将GET 设置为synchronous,但它似乎没有改变任何东西。是scope 问题吗?好像addScrollListener是纯JS方法,不能附加到angular2容器上?

【问题讨论】:

    标签: javascript angular typescript ionic2


    【解决方案1】:

    是的,这是一个范围问题。您应该使用箭头函数来保留此上下文

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

    【讨论】:

    • 谢谢,它就像一个魅力:)。你有这方面的文件吗?
    猜你喜欢
    • 2016-12-07
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 2016-12-25
    • 2017-06-07
    • 1970-01-01
    • 2019-10-17
    • 1970-01-01
    相关资源
    最近更新 更多