Dears,
I am stuck, i have followed simple angular.io tutorial for http.get and observables, but i am not able to render the data in the view.
My service:
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { Observable } from "rxjs";
import { map } from "rxjs/operators";
const baseUrl = "http://localhost:4000";
const fetchAllHeroesUrl = "/heroes";
@Injectable({
providedIn: "root",
})
export class HeroesService {
constructor(private http: HttpClient) {}
getHeroes(): Observable<any> {
return this.http.get(baseUrl + fetchAllHeroesUrl);
}
}
My component:
import { Component, OnInit } from "@angular/core";
import { HeroesService } from "./../shared/services/heroes.service";
import { Observable } from "rxjs";
@Component({
selector: "app-hero-list",
templateUrl: "./hero-list.component.html",
styleUrls: ["./hero-list.component.scss"],
})
export class HeroListComponent implements OnInit {
public listOfHeroes$: any;
constructor(private heroesService: HeroesService) {}
ngOnInit() {
this.getAllHeroes();
}
getAllHeroes(): void {
this.heroesService
.getHeroes().subscribe((value: any) => this.listOfHeroes$ = value.data);
}
}
my template:
<mat-list>
<mat-list-item *ngFor="let hero of listOfHeroes$ | async">
<img
matListAvatar
src="hero.avatar_url"
alt="hero.full_name">
<h3 matLine>{{hero.full_name}}</h3>
<p matLine>
<span>{{hero.description}}</span>
<span class="demo-2">-- {{hero.type}}</span>
</p>
</mat-list-item>
</mat-list>
but still nothing is showing in the view (in webbrowser, also i have checked Network tab in dev tools, and the data come from my local erver), whenever i console.log(this.listOfHeroes$) i get undefined Shouldn't this be working? am i missing something?