Skip to content

Instantly share code, notes, and snippets.

@saimon24
Created August 9, 2019 11:47
Show Gist options
  • Save saimon24/ebc33c80b5fb0777291ae8f93c4fa8ec to your computer and use it in GitHub Desktop.
Save saimon24/ebc33c80b5fb0777291ae8f93c4fa8ec to your computer and use it in GitHub Desktop.
import { PokemonService } from './../services/pokemon.service';
import { Component, OnInit, ViewChild } from '@angular/core';
import { IonInfiniteScroll } from '@ionic/angular';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage implements OnInit {
offset = 0;
pokemon = [];
@ViewChild(IonInfiniteScroll) infinite: IonInfiniteScroll;
constructor(private pokeService: PokemonService) { }
ngOnInit() {
this.loadPokemon();
}
loadPokemon(loadMore = false, event?) {
if (loadMore) {
this.offset += 25;
}
this.pokeService.getPokemon(this.offset).subscribe(res => {
this.pokemon = [...this.pokemon, ...res];
if (event) {
event.target.complete();
}
// Optional
if (this.offset == 125) {
this.infinite.disabled = true;
}
});
}
onSearchChange(e) {
let value = e.detail.value;
if (value == '') {
this.offset = 0;
this.loadPokemon();
return;
}
this.pokeService.findPokemon(value).subscribe(res => {
this.pokemon = [res];
}, err => {
this.pokemon = [];
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment