Angular 6 Promise两次调用一个函数

时间:2018-07-31 16:27:50

标签: angular6

我是Angular的新手,目前正面临Promises的问题。调用showMoreClickedHandler()时,函数getContents()被调用两次。在其他情况下,getContents()仅被调用一次。我检查了断点,当const reqGetContentsPromise未定义时,它看起来回到了第1行。下面是代码。任何帮助将不胜感激!

getContents(): Promise<Content[]> {
    console.log('Contents called twice');
    this.getContentsCalled = true;
    this.spinner.show();



    const reqGetContentsPromise = this.salesforceService.getContents(
        this.pageNum,
        this.resultsPerPage,
        this.sortBy,
        this.sortDir,
        this.filterType,
        this.filterVal,
        this.searchVal,
        this.courseId
    );

    reqGetContentsPromise.then(
        resData => {
            this.contents = this.contents.concat(resData);
        },
        err => {
            this.contents = [];
        }
    );

    return reqGetContentsPromise;
}

showMoreClickedHandler():无效{

    console.log('Show more called');

    this.pageNum++;
    this.getContents();
    $('#search-content-div').animate({ scrollTop: $('#search-content-div').prop('scrollHeight') }, 500);
}

0 个答案:

没有答案