Created
August 9, 2019 11:47
-
-
Save saimon24/ebc33c80b5fb0777291ae8f93c4fa8ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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