1

Hi im new in angular and i want to refresh a component after i add a new set of API url after the click event in the button this is my code. pokemons.service.ts

export class PokemonsService { private _url: string = "https://pokeapi.co/api/v2/pokemon"; private _pokemonUrl: string; constructor(private http : HttpClient) { } setUrl(value: string){ this._url = value; } getUrl(): Observable<IPokemon[]>{ return this.http.get<IPokemon[]>(this._url); } setPokemonUrl(value: string){ this._pokemonUrl = value; } getPokemonsUrl(): Observable<IPokemonDetails[]>{ return this.http.get<IPokemonDetails[]>(this._pokemonUrl); } } 

pokemon-search.component.ts

 export class PokemonSearchComponent implements OnInit { pokemons = []; pokemonUrl: string; constructor(private _pokemons : PokemonsService) { } ngOnInit(){ this._pokemons.getUrl().subscribe(data => { console.log(data); this.pokemons = data; }); } sendPokemonUrlToService(value: string){ this._pokemons.setPokemonUrl(value); } onClick(url){ this._pokemons.setPokemonUrl(url); console.log(this._pokemons); } onClickNextPokemon(url){ this._pokemons.setUrl(url); console.log(this._pokemons); } } 

pokemon-search.component.html

 <ul *ngFor="let pokemon of pokemons.results"> <li><button (click)="onClick(pokemon.url)">{{pokemon.name}}</button></li> </ul> <button (click)="onClickNextPokemon(pokemons.previous)">PREVIOUS</button> <button (click)="onClickNextPokemon(pokemons.next)">NEXT</button> 

thankyou so much in advance

5
  • 1
    There is no need to refresh component, Angular always keep your data binding updated. Can you please tell the issue in your code? Is it possible for you to create a stackblitz instance? Commented Apr 11, 2020 at 6:51
  • You need rxjs-subject. When user search for url that url goes to rxjs-subject and all other consumer of this subject gets results. This way your main data api and search api works separately with single responsibility. Commented Apr 11, 2020 at 7:12
  • its good now, but right now after i solve that i have a problem like this. Commented Apr 11, 2020 at 7:39
  • Can't bind to 'ngForOf' since it isn't a known property of 'ul'. Commented Apr 11, 2020 at 7:40
  • have you import ngModule in app.module.ts and common module to feature module? Commented Apr 11, 2020 at 8:28

1 Answer 1

1

In general, your UI should be updated when data changes happened. If u are using changeDetection: ChangeDetectionStrategy.OnPush as your change detection strategy and your data changes not reflected in your UI, U can use mark for check for that like below

 constructor(private _pokemons : PokemonsService, private _changeDetectorRef: ChangeDetectorRef) { } ngOnInit(){ this._pokemons.getUrl().subscribe(data => { console.log(data); this.pokemons = data; this._changeDetectorRef.markForCheck(); }); } 

make sure your data is available this._pokemons.getUrl() in this method call.


add your *ngFor like below. you should loop li instead of ul

<ul> <li *ngFor="let pokemon of pokemons.results"> <button (click)="onClick(pokemon.url)">{{pokemon.name}}</button> </li> </ul> 
Sign up to request clarification or add additional context in comments.

2 Comments

its good now, but right now after i solve that i have a problem like this. "Can't bind to 'ngForOf' since it isn't a known property of 'ul'." can you please help me? i alr add the BrowserModule and still nothings happening
*ngFor should be in li not ul

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.