Trying to obtain values from an observable, my subscription code within the component looks like this:
this.checkoutService.getDisabledDate().subscribe
(dates=>{this.formattedDate=dates},
(error:any)=>{console.log(error)});
When logging this.formattedDate in the 'dates' callback, it correctly shows the values. However, trying to access this.formattedDate outside of the subscription results in undefined.
This is 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 attempted the same process by passing data through a queryParam using short form (), but it did not make any difference. It seems I am missing something crucial in order to extract the information with this method.
I've referred to the following resources for help: Angular2 HTTP using observables subscribe showing data undefined and Angular 2 return data from service is not availabe after RxJs subscribe.
Despite going over these threads, I still cannot figure out what I am overlooking. Any ideas on what I might be missing?