I've got this httpclient request in an angular 4.3 service:
updateTermin(termin: Termin){
let url = '${this.termineUrl}/${termin.id}';
this.http
.put(url, termin, {headers: this.headers})
}
it only gets fired if i append a .subscribe() like this:
updateTermin(termin: Termin){
let url = '${this.termineUrl}/${termin.id}';
this.http
.put(url, termin, {headers: this.headers})
.subscribe()
}
The other request (delete or post) work without append .subscribe(). Why?
Passing multiple parameters to Http get request We have to pass page & per_page parameters to the list of users API. let queryParams = new HttpParams(); queryParams = queryParams. append("page",1); queryParams = queryParams. append("per_page",1);
HttpClient is a built-in service class available in the @angular/common/http package. It has multiple signature and return types for each request. It uses the RxJS observable-based APIs, which means it returns the observable and what we need to subscribe it.
Use the HttpClient.get() method to fetch data from a server. The asynchronous method sends an HTTP request, and returns an Observable that emits the requested data when the response is received. The return type varies based on the observe and responseType values that you pass to the call.
get() method is an asynchronous method that performs an HTTP get request in Angular applications and returns an Observable. And that Observable emits the requested data when the response is received from the server.
The HttpClient
request methods (all of them) return an Observable: basically it's a chain of commands waiting to be executed. Nothing happens until you call subscribe()
on that chain. So this.http.put(...)
only prepares the commands for you.
If you're coming from an environment that uses promises, this will seem counter-intuitive because a promise executes as soon as it comes alive. Observables are different. Check out this short intro on YouTube
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With