Firstly, I would encourage you to create interfaces for your data models. They don't have to match what your API returns, but in your case it makes sense because the API response is well named.
export interface Stock {
name: string;
exchange: string;
}
Create a service that will make your API calls. The service won't invoke the http request itself, it will just return the observable. The service knows how it is going to make the call, and the component knows when the call should be made.
stock.service.ts
fetchData(): Observable<Stock[]> {
return this.http
.get('https://financialmodelingprep.com/api/v3/company/stock/list')
.pipe(
map((response: any) => response.symbolsList)
);
}
Inject the service into your component and invoke the call whenever you want to. Set a public property so that your HTML can "see" it.
component.ts
export class MyComponent {
constructor(private stockService: StockService) { }
stocks: Stock[];
onFetchData() {
this.stockService.fetchData().subscribe((stocks: Stock[]) => {
this.stocks = stocks;
});
}
}
Then bind to the data in your HTML:
component.html
<div *ngFor="let stock of stocks">
{{stock.name}}: {{stock.exchange}}
</div>
DEMO: https://stackblitz.com/edit/angular-waymkr