admin管理员组文章数量:1322038
My Service :
fetchData(url,request)
{
return this.http.post(this.apiUrl+url,request).subscribe(
(response) => {return response.json()}
);
}
My Component :
ngOnInit()
{
this.response = this.dataService.fetchData('friends/grow_network',{});
console.log('s ',this.response);
}
if I console response.json() in service it shows proper data which is ing from api but if I console in ponent it shows like this :
Subscriber {closed: false, _parent: null, _parents: null, _subscriptions: Array(1), syncErrorValue: null…}
how to get data in ponent which is ing from api not Subscriber data ?
My Service :
fetchData(url,request)
{
return this.http.post(this.apiUrl+url,request).subscribe(
(response) => {return response.json()}
);
}
My Component :
ngOnInit()
{
this.response = this.dataService.fetchData('friends/grow_network',{});
console.log('s ',this.response);
}
if I console response.json() in service it shows proper data which is ing from api but if I console in ponent it shows like this :
Subscriber {closed: false, _parent: null, _parents: null, _subscriptions: Array(1), syncErrorValue: null…}
how to get data in ponent which is ing from api not Subscriber data ?
Share Improve this question edited Jul 27, 2017 at 6:51 georgeawg 49k13 gold badges77 silver badges98 bronze badges asked Jul 27, 2017 at 6:42 Mohit BumbMohit Bumb 2,4935 gold badges33 silver badges54 bronze badges2 Answers
Reset to default 2When doing it your way, you write in the variable "reponse" the observable object and not the result. The value of the result is not there yet when calling the method, because it will be asynchronous.
To get what you want you have to do the following:
fetchData(url,request)
{
return this.http.post(this.apiUrl+url,request).map(
(response) => {return response.json()}
);
}
ngOnInit()
{
this.response = this.dataService.fetchData('friends/grow_network',{})
.subscribe(result => {
this.response = result;
console.log('s ',this.response);
});
}
You can using Observables in angular . and please checkout this below discussion
What is the difference between Promises and Observables?
Now try this below code instead of your code in ponent
this.response = this.dataService.fetchData ('friends/grow_network'{})
.subscribe(res=> { console.log(res);
}), error => alert(error);
and you service code should be
fetchData(url,request)
{
return this.http.post(this.apiUrl+url,request).map(
(response) => {return response.json()}
);
}
版权声明:本文标题:javascript - How to return data coming from response instead of subscription json in Angular 24 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742108667a2421142.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论