klp klp - 2 months ago 9
TypeScript Question

How to get the values of the subscribed data in angular2

I wrote a injectable service in which i want to return "display" in my data and i done it sucessfully as follows,

export class GetAllList {
str = localStorage.getItem('social');
loc = JSON.parse(this.str);
id = this.loc._id;
private _productUrl = 'http://localhost:3000/getprofiledetails/'+this.id;

constructor(private _http: Http) { }

getList(): Observable<IDetails[]> {
return this._http.get(this._productUrl)
.map((response: Response) => {
return response.json().data.display;

});

}
}


Here i am subscribing to it,

this._profileservice.getList()
.subscribe(
details1 => this.details1 = details1);
console.log("displaystas:"+this.details)


The problem is,my console is displaying undefined?so how can i see my display value in my console?Can anyone suggest me help.Thank you.

Answer

You are printing wrong variable (details instead of details1) and you are missing {}:

this._profileservice.getList()
    .subscribe(
        details1 => {
        this.details1 = details1;
        console.log("displaystas: " + this.details1)
        }