I need advice on how to make observable variable currentShop
dependent on variable shopList
.
AppComponent
reads shopList
through shopProvider
. It works without a problem, but I need to modify the code to set currentShop
from loaded shops.
AppComponent:
export class AppComponent {
currentShop;
shopList;
constructor(router: Router,shopProvider:ShopProvider){
this.shopList = shopProvider.shops;
this.currentShop = shopProvider.currentShop;
shopProvider.loadShops();
shopProvider.setCurrentShop(-1);
router.navigate(['/System']);
}
}
ShopProvider:
export class ShopProvider{
private _http:Http;
shops:Observable<Array<Shop>>;
currentShop:Observable<Shop>;
private obServer:any;
private _shopDataStore : {
shops: Array<Shop>
}
constructor(http:Http){
this._http = http;
this.shops = new Observable(observer => this.obServer = observer).share();
//this.currentShop = ??
this._shopDataStore = {shops: []};
}
setCurrentShop(shopId:number){
//Code ??
}
loadShops(){
this._http
.get('/services/shop/list')
.map(res => res.json())
.subscribe(data => {
this._shopDataStore.shops = data;
this.obServer.next(this._shopDataStore.shops);
})
}
}
HTML:
<div>{{currentShop.name | async}}</div>
<ul>
<li *ngFor="#shop of shopList | async">
<a [routerLink]="['/Shop', {shopId: shop.id}]">
<span>{{shop.name}}</span>
</a>
</li>
</ul>