2
別のstore location
に移動するとテンプレートに問題があります。 (例)デフォルトはEastleigh
です。USA
に移動するとUSA
に変わりますが、別の場所に行くとUSA
に行きました。angular2でrouterLinkを使用してテンプレートを更新
テンプレートを更新するには、Collection Store - {{}}
というようにページを更新する必要がありますが、そうではありません。
私のテンプレートhtmlにはこれがあります。
<span> Collection Store - {{ currentStore?.Name }}</span>
場所バックエンドから返されている - このサービスを使用して:
public storesChanged = new BehaviorSubject([]);
public getStores(): void {
this.retrieveResults().subscribe((results) => {
this.storesChanged.next(results.Results)
console.log('Name >> ', results.Results[0].Name) // this shows me the Name
});
}
public retrieveResults(): Observable<any> {
return this.http.get('/api/Stores/Summary')
.map(res => res.json())
}
コンポーネント -
ngOnInit() {
this.routeSub = this.route.params.subscribe(params => {
this.storeId = +params['storeId'];
this.storeLocation = params['storeLocation'];
if (!this.storeId) {
this.storeId = DEFAULT_ORIGIN_ID;
}
})
this.sharedService.storesChanged.subscribe((res) => {
this.currentStore = res.find((x) => x.Id === this.storeId);
console.log('Change Location >> ', this.currentStore)
})
}
これは、私がドロップダウンメニューのURLをレンダリングする方法を
<li *ngFor="let store of sourceSites | slice:1" [class.active-location]="storeId === store.Id"><a id="{{store.Name}}" [routerLink]="['/order-screen/store/' + store.Id + '/' +store.Name]">{{ store.Name}}</a></li>
です
canon私がレンダリングするテンプレートcurrentStore?.Name
が必ずしも更新されていないのはなぜですか?私は別のURL /場所を選択するために変更するたびにページを更新したくありません。
固定
ngOnInit() {
this.routeSub = this.route.params.subscribe(params => {
this.storeId = +params['storeId'];
this.storeLocation = params['storeLocation'];
if (!this.storeId) {
this.storeId = DEFAULT_ORIGIN_ID;
}
this.sharedService.storesChanged.subscribe((res) => {
this.currentStore = res.find((x) => x.Id === this.storeId);
console.log('Change Location >> ', this.currentStore)
})
});
}
私は既にそれを購読しています、申し訳ありません上記のことを忘れてしまった。私は私の質問にコードを追加しました。 – MrNew
私は今それを修正したと思います。私の 'storeChanged'サービスは、' params.subscribe'の内側にある必要があります。 – MrNew