Search code examples
angularrequesthttpclientput

Angular 4.3 HttpClient put doesn't fire


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?


Solution

  • 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