Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4.3 HttpClient put doesn't fire [duplicate]

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?

like image 208
volkit Avatar asked Oct 24 '17 17:10

volkit


People also ask

How do you pass multiple parameters in HTTP GET request in Angular 8?

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);

Why is HttpClient return observable?

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.

What is the use of HttpClient get () method?

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.

What is the return type of get () method of HttpClient API?

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.


1 Answers

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

like image 98
BeetleJuice Avatar answered Sep 27 '22 20:09

BeetleJuice