I'm trying to extract the values from an observable, my subscription (component) code is as followed:
this.checkoutService.getDisabledDate().subscribe
(dates=>{this.formattedDate=dates},
(error:any)=>{console.log(error)});
In the 'dates' callback a console.log of this.formattedDate prints the correct values. However trying to access this.formattedDate outside of the subscription is undefined.
the service code:
getDisabledDate():Observable<any>{
let headers = new Headers({'Content-Type': 'application/json' });
let options = new RequestOptions({headers:headers});
let userRequest={action_id:0};
let disabledDate={};
return this.http
.post(this.deliveryUrl,userRequest,options)
.map((r:Response)=>r.json())
.catch(this.handleError);
}
I've performed the same action passing data over a queryParam using short form (), and it made no difference in this case. I seem to be overlooking what is necessary to pull out the information with this one.
I've looked at both:
Angular2 HTTP using observables subscribe showing data undefined
and
Angular 2 return data from service is not availabe after RxJs subscribe.
Which I'm passed where their questions were answered. What am I missing?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…