[英]Observable in Angular returns [ Object object ]
我是Angular的新手。 我在這里看到了類似的問題,但仍然無法理解。 為我服務:
banner(): Observable<String> {
return this.http.get<String>(`${SERVER_API_URL}/api/banner`);
}
在組件中:
ngOnInit() {
this.principal.identity().then(account => {
this.account = account;
});
this.registerAuthenticationSuccess();
this.craCoreService.banner().subscribe(value => this.banner = value);
}
和html:
<div class="alert alert-warning" *ngSwitchCase="false">
{{banner}}
</div>
我的服務方法返回具有單個String屬性(稱為“橫幅”)的簡單JSON對象。 但是在UI中,我總是得到[Object object]。 我不明白如何從中獲取價值 。 感謝您的幫助,謝謝。
如果它是帶有banner
屬性的對象,那么您需要在TS代碼中使用該use屬性
this.craCoreService.banner().subscribe(value => this.banner = value.banner);
使用json
管道在模板中顯示對象。
<div class="alert alert-warning" *ngSwitchCase="false"> {{banner | json}}</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.