Вы можете создать сервис в этом сервисе, вам нужно добавить это:
import { HttpClient } from '@angular/common/http';
export class RequestService {
constructor(public http: HttpClient) {}
apiUrl : string = "http://localhost:5000";
getResponse() : Observable<string>{
return this.http.get<string>(this.apiUrl+"/calledMethod");
}
}
в вашем app.component.ts :
import { RequestService } from './request.service'
import { Component, OnInit } from '@angular/core';
export class AppComponent implements OnInit{
stringResult : string = ""
constructor(private request : RequestService) {}
ngOnInit()
{
this.showResponse()
}
showResponse(){
this.request.getResponse().subscribe(
r => this.stringResult = r,
err => console.log(err)
)
}
}
Кфинишируйте в app.component.html :
{{stringResult}}
Пример: Запрос API