admin管理员组文章数量:1327102
I am trying to get an item from an observable in Angular 2 that's provided by a service. So far I can subscribe to the observable and see the contents in the console but cannot filter out an item by it's ID. I have tried the following below but get a blank screen. Any tips appreciated.
service.ts:
getJobs() {
this.observableData = this._http.get('json_path/')
.map(res => res.json())
.do(val => {
this.result = val;
this.observableData = null;
})
.share();
return this.observableData;
}
ponent.ts:
let id = this._routeParams.get('id');
this._jobService.getJobs().map(jobs => jobs.filter(item => item.id === id).subscribe(job => this.job = job)[0]);
I am trying to get an item from an observable in Angular 2 that's provided by a service. So far I can subscribe to the observable and see the contents in the console but cannot filter out an item by it's ID. I have tried the following below but get a blank screen. Any tips appreciated.
service.ts:
getJobs() {
this.observableData = this._http.get('json_path/')
.map(res => res.json())
.do(val => {
this.result = val;
this.observableData = null;
})
.share();
return this.observableData;
}
ponent.ts:
let id = this._routeParams.get('id');
this._jobService.getJobs().map(jobs => jobs.filter(item => item.id === id).subscribe(job => this.job = job)[0]);
Share
Improve this question
edited Apr 10, 2017 at 21:49
skyscript
asked Apr 25, 2016 at 17:10
skyscriptskyscript
1753 silver badges12 bronze badges
2
-
Is this intentional. You posted the
getItems()
method and then use thegetJobs()
method below. – Günter Zöchbauer Commented Apr 25, 2016 at 17:11 - Ah, that was a mistake. They should both be either getItems() or getJobs(). Thanks – skyscript Commented Apr 25, 2016 at 21:07
1 Answer
Reset to default 6I think you want something like:
let id = this._routeParams.get('id');
this._jobService.getJobs().map(jobs => {
return jobs.filter(item => item.id === id)[0];
}).subscribe(job => this.job = job));
You can't subscribe to the result of jobs.filter()
本文标签: javascriptGet item from Observable in Angular 2 with TypescriptStack Overflow
版权声明:本文标题:javascript - Get item from Observable in Angular 2 with Typescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742205990a2432826.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论