Как сделать, чтобы API не загружался, если в angular нет данных с использованием global.getData - PullRequest
0 голосов
/ 26 марта 2020

вот код:

loadNextBatch() {
    console.log('scrolldown');

    this.pageIndex = this.pageIndex + 1;
    this.global.getData(`/conditions/latest?start=${this.pageIndex}&length=${this.pageSize}`)
      .pipe(take(1)).subscribe(res => {

        const newBatch = res['data'];
        if (newBatch.length === 0) {
          return false;
        }

        this.tempThermometer.next(this.tempThermometer.getValue().concat(newBatch));
      console.log(this.tempThermometer);
      });
  }

что я пытаюсь сделать здесь, когда нет данных от /conditions/latest?start=9&length=4, который все еще работает до start=11 ...etc, как заставить его остановиться. когда данных нет, их следует останавливать в start=8, потому что в start=9 данных больше нет.

Как остановить его с start=8, но когда есть данные на start=9, его можно загрузить. но если на start=9 нет данных, они не должны быть загружены.

также проблема, когда я прокручивал вниз, она продолжала до start=11`` start = 12 it should be stop on start = 8 cause there's no data on start = 9 `` `

1 Ответ

0 голосов
/ 26 марта 2020

Определил глобальную переменную и обернул вызов API в if condition кажется логичным, верно?

canLoadNextbatch = true;

loadNextBatch() {
  console.log('scrolldown');

  this.pageIndex = this.pageIndex + 1;
  if (this.canLoadNextbatch) {
    this.global.getData(`/conditions/latest?start=${this.pageIndex}&length=${this.pageSize}`)
      .pipe(take(1)).subscribe(res => {

        const newBatch = res['data'];
        if (newBatch.length === 0) {
          this.canLoadNextbatch = false;
        } else {
          this.tempThermometer.next(this.tempThermometer.getValue().concat(newBatch));
          console.log(this.tempThermometer);
        }
      });
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...