バックエンドから返されたJSONをHTML {{ object.x }}
にレンダリングしようとしていますが、私はBehaviourSubject
を共有サービスで使用しています。レンダリングしようとしている。HTMLでJSONをAngular2でレンダリングしようとしています
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())
}
このサービスを使用して:
this.sharedService.storesChanged.subscribe((res) => {
this.currentStore = res.find((x) => x.Id === this.storeId)
// this returns the object based on the Id which should be able to use in my HTML
})
これは、エラーCannot read property 'Name' of undefined
を返していますが、私はHTML {{ currentStore.Name }}
でレンダリングしようとしているオブジェクトです。
{
'Id': 1,
'Name': 'Eastleigh'
}
なぜプロパティーがオブジェクト内にあるのかわかりません。誰かが原因を指摘できますか?あなたのcurrentStore
オブジェクトが非同期的に受信されているので
'{{currentSore.Name}}' - あなたはこの質問 – echonax
を持って申し訳ありませんうんそれは私自身のタイプミスだしようとしましたエルビスオペレーター? '{{currentStore?.Name}}' – MrNew
を入力中に> '{{currentStore.Name}}'あなたがタイプミス – echonax