当我移动到另一个模板时,我遇到了模板未更新的问题store location
。 (例如)默认值为Eastleigh
如果我搬到USA
它将更改为USA
但当我去其他地方时它就粘住了USA
.
我必须刷新页面以便模板更新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)
})
}
这就是我渲染下拉菜单网址的方式,
<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>
有人可以指出为什么我渲染的模板currentStore?.Name
不是一直在更新吗?我不想每次更改以选择不同的网址/位置时都刷新页面。
Fixed
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)
})
});
}