【发布时间】:2018-10-13 17:31:54
【问题描述】:
我正在尝试从通过打字稿中的 http.get() 调用的 URL 获取详细信息。我检索此详细信息的 URL 端点是一种 localhost:8001\character\1234567890,其中数字是字符的 ID。
如果我在浏览器上测试,URL 会正确返回所有数据。
我的问题在于 typescript 函数调用。在 HTML 前面,我将函数调用为:
<a href="#{{character.id}}" (click)="characterDetail(character.id)">Read more</a>
那么,剩下的函数就是这些了:
characterDetail(characterId: number) {
this.asyncCharDetail = this.getCharacterDetail(characterId)
.do(res => {
console.log(res);
})
.map(res => res.results);
}
getCharacterDetail(characterId: number) {
return this.http.get(this.characterDetailApiUrl + characterId,{})
.map((res: Response) => res.json())
}
第一个函数中的console.log(res) 不打印任何内容。 characterId 和 url 都是正确的,但不知道没有检索字符详细信息的原因。
数据检索的第二部分是在模态窗口上显示。
编辑:这里有更多代码:
private characterDetailApiUrl = 'http://localhost:8001/character/';
....
//To save the character details from Http get.
asyncCharDetail: Observable<string[]>;
characterDetail(characterId: number) {
console.log('Character detail for ID ' + characterId);
this.loading = true;
this.asyncCharDetail = this.getCharacterDetail(characterId)
.do(res => {
console.log(res);
this.loading = false;
})
.map(res => res.results);
console.log(this.asyncCharDetail);
}
getCharacterDetail(characterId: number) {
return this.http.get(this.characterDetailApiUrl + characterId )
.map((res: Response) => res.json());
}
关于 Html 前端文件:
<a href="" (click)="characterDetail(character.id); $event.preventDefault()">Read more</a>
【问题讨论】:
标签: angular typescript http-get