angular - assigned value to local variable inside subscribe() -


i making http post , trying assign value local variable inside subscribe.

however local variable line of code gets never called. why ? whats wrong code?

this component

submitted = false;      onsubmit() {     this.leaveservice.addleave(this.model)         .subscribe(res => {             this.submitted = true;  **// line of code never execute. why?**          });      } 

this service

addleave(body: leave): observable<leave[]> { let bodystring = json.stringify(body); // stringify payload let headers = new headers({ 'content-type': 'application/json' }); // ... set content type json let options = new requestoptions({ headers: headers }); // create request option  return this.http.post(this._apiurl, body, options)     .map(res => res.json())     .catch((error: any) => observable.throw('error')); } 

my mvc controller method (just added simple 1 ensure round trip fine)

[httppost("[action]")] public iactionresult createleaverequest([frombody] leavemodel leave) {     return statuscode(200); } 

}

i don't see error in console. let me know if need more information understand problem.

note:

onsubmit() called. sequence calls onsubmit(), calls addleave service, sends request controller method (debug point hit) .

your .map function needs return something...

return this.http.post(this._apiurl, body, options)     .map(res => {       console.log(res) // log here see in response body       return res.json() || []; // if response has no json return []     )      .catch((error: any) => observable.throw('error')); } 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -