0
我使用ASP.NET Core和Angular 2(版本〜2.2.0)和Rxjs(版本^ 5.0 0.0-rc.5)。 angular.io文檔中的代碼示例適用於Angular 2的In Memory Web API,但是當我向httpd.get調用C#控制器時,它會失敗。數據正在返回。下面的代碼行不起作用:Angular 2 RxJs Promise對http.get調用C#控制器不起作用,而Angular 2在內存Web API中起作用
getHeroes(): Promise<Hero[]> {
return this.http.get(this.heroesUrl)
.toPromise()
.then(response => response.json().data as Hero[])
.catch(this.handleError);
}
我已經改變了代碼和這些代碼以下行做的工作:
getHeroes(): Hero[] {
this.http.get(this.heroesUrl).map(response => response.json()).subscribe(heroes => {
this.heroes = heroes;
})
return this.heroes;
}
誰能幫助?我真的需要獲得第一行代碼才能工作,因爲第二行代碼無法按計劃運行,只是表明數據正在返回並顯示。
請說明您的具體問題或添加額外的細節,突顯正是你需要的。正如目前所寫,很難確切地說出你在問什麼。請參閱「如何問問」頁面以獲取有關澄清此問題的幫助。 – olsn
爲什麼你在一個地方使用'response.json()。data',而在另一個地方只使用'response.json()'?它不應該一樣嗎? – chrigu
從代碼的第二個例子可以清楚的看到,數據是從C#Controller返回的,並且由Angular 2顯示。第一個代碼示例來自angular.io文檔,Promise不起作用。我可能會將Promise更改爲Observable以使其起作用。我不清楚爲什麼Promise不起作用。 – Mark