У меня проблема с тем, что мой шаблон не обновляется, когда я перехожу на другой store location
. (например). Значение по умолчанию: Eastleigh
, и если я перейду к USA
, он изменится на USA
, но когда я перейду в другое место, оно будет зависеть от USA
.шаблон обновления с использованием routerLink в angular2
Мне нужно обновить страницу, чтобы шаблон обновил 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)
})
}
Это, как я визуализировать URLs меню раскрывающийся,
<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>
Может кто-нибудь e указать, почему шаблон, в котором я показываю 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