Hello, I am new to Angular and I am trying to pass a URL from a component and set it as the new service URL. Here is my code:
pokemon.service.ts
private _url: string = "https://pokeapi.co/api/v2/pokemon";
constructor(private http : HttpClient) { }
setUrl(value: string){
this._url = value;
}
getUrl(): Observable<IPokemon[]>{
return this.http.get<IPokemon>(this._url);
}
pokemon-search.component.ts
export class PokemonSearchComponent implements OnInit {
pokemons: any = [];
pokemonUrl: string;
constructor(private _pokemons : PokemonsService ) { }
ngOnInit(){
this._pokemons.getUrl().subscribe(data => {
this.pokemons = data;
});
}
previousPokemon(url){
this._pokemons.setUrl(url);
}
}
I am passing the URL back to the service but it doesn't change the output of the default URL. Can someone please help? Thank you!.