class MyService {
private _list1;
private _list2;
getList1() {
if(this._list1) {
return Observable.of(this._list1);
} else {
return _getList(1);
}
}
getList2() {
if(this._list2) {
return Observable.of(this._list2);
} else {
return _getList(2);
}
}
private _getList(listNo) {
return this.http.get(url).map(res => {
var json = res.json();
this._list1 = value.list1;
this._list2 = value.list2;
if(listNo == 1) {
return json.list1;
}
if(listNo == 2) {
return json.list2;
}
});
}
}
@Component({
selector: 'my-component1',
pipes: [KeysPipe],
template: `
<ul>
<li *ngFor="let item of list | keys">{{item.key}} - {{item.value}}</li>
</ul>
`
})
export class MyComponent1 {
constructor(private MyService myService) {
myService.getList1().subscribe.(value => this.list = value);
}
}
@Component({
selector: 'my-component2',
pipes: [KeysPipe],
template: `
<ul>
<li *ngFor="let item of list | keys">{{item.key}} - {{item.value}}</li>
</ul>
`
})
export class MyComponent2 {
constructor(private MyService myService) {
myService.getList2().subscribe.(value => this.list = value);
}
}
If you want to render both lists in the same component it can be simplified of course.
For the KeysPipe
see access key and value of object using *ngFor